简体   繁体   English

Pandas 数据框合并但创建 NaN 值

[英]Pandas dataframe merging but creating NaN values

I have two pandas dataframes with one column in each which looks like:我有两个 Pandas 数据框,每列有一列,如下所示:

df1                        df2
Price                     volume
 20                        200
 12                        50
 43                        10
 45                        5

When I try to merge the two dataframes using: new_df = pd.concat([Volume, Price], axis=1) i get当我尝试使用以下方法合并两个数据框时: new_df = pd.concat([Volume, Price], axis=1) 我得到

    Volume      Price
3   20           NaN
4   NaN          200
5   12           NaN
6   NaN          50
7   43           NaN

I've tried using pd.concat / pd.merege / pd.join but can't seem to get a clean merge.我试过使用 pd.concat / pd.merege / pd.join 但似乎无法进行干净的合并。 whats the best way to go about fixing this?解决这个问题的最佳方法是什么?

df1 = pd.DataFrame({'Price': [20, 12, 43, 45]})
df2 = pd.DataFrame({'volume': [200, 50, 10, 5]})

new_df = pd.concat([df1, df2], axis=1)

It works for me and I do not get any NaN values.它对我有用,我没有得到任何 NaN 值。 Use dataframe names in pd.concat, not column names.在 pd.concat 中使用数据框名称,而不是列名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM