Trying to combine two data frames

Question:

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

Asked By: acidic231

||

Answers:

By combining, if you meant appending; then try this… means daya y below data x;

Americas = pd.concat([x,y])
Answered By: Sachin Kohli

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'])]
Answered By: abokey
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.