I'm trying to combine two dataframes. This is my code:
x = df.loc[df['continent'] == 'South America']
y = df.loc[df['continent'] == 'North America']
Americas =x y
Americas
When this prints, it just gives back NaN values
CodePudding user response:
By combining, if you meant appending; then try this... means daya y below data x;
Americas = pd.concat([x,y])
CodePudding user response:
The reason why you're getting NaN
values is that you're making a binary add
(which is equivalent to pandas.DataFrame.add
) between two dataframes that have not the same indexes. Try to add .reset_index()
to both x
and y
and you'll see a different behaviour.
To achieve what you're looking for, you can use pandas.concat
instead :
Americas = pd.concat([x,y], ignore_index=True))
Or simply pandas.Series.isin
if there is no need to the intermediates dataframes :
Americas = df.loc[df['continent'].isin(['South America', 'North America'])]