Relative Content

Tag Archive for python-3.xpandasdataframe

How do you one hot encode a column that exists in multiple pandas dataframes?

I have ten dataframes of event level data from ten different hockey seasons that each have a couple columns that I’d like to one hot encode with the intention of training a model on the historical data to make predictions about this years data . My current issue is that that columns that need to be encoded don’t contain all possible values in each dataframe. Just as an example there have been some expansion teams added, so dataframes for earlier years would not have these teams and thus they would not have an encoded column.

Getting NameError for pandas dataframe

I have df1 with column ‘col’ & df2 with columns ‘code’ and ‘id’. Column ‘col’ in df1 is mapped to ‘code’ column in df2. When I am running the below code:

Pandas Merge two DFs on common ID

I’m merging two dataframes, one has 3 rows and the other has 100+ rows. What I am attempting to do is look at the IDs in DF1 and grab the corresponding ID data in DF2 and merge them together, ignoring the other rows. What seems to be happening it is just pulls in the first 3 rows form DF2 and isn’t matching on the IDs.