[英]Trying to combine two data frames
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打印时,它只是返回 NaN 值
By combining, if you meant appending;通过组合,如果你的意思是追加; then try this... means daya y below data x;
然后试试这个...意味着数据 x 下的 daya y;
Americas = pd.concat([x,y])
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.你得到
NaN
值的原因是你在两个没有相同索引的数据帧之间进行二进制加+
(相当于pandas.DataFrame.add
)。 Try to add .reset_index()
to both x
and y
and you'll see a different behaviour.尝试将
.reset_index()
添加到x
和y
,您会看到不同的行为。
To achieve what you're looking for, you can usepandas.concat
instead:要实现您正在寻找的目标,您可以改用
pandas.concat
:
Americas = pd.concat([x,y], ignore_index=True))
Or simply pandas.Series.isin
if there is no need to the intermediates dataframes:或者简单地
pandas.Series.isin
如果不需要中间数据帧:
Americas = df.loc[df['continent'].isin(['South America', 'North America'])]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.