简体   繁体   English

通过匹配索引合并两个数据框

[英]Combine two data frames by matching index

I have a data frame that has the form:我有一个具有以下形式的数据框:

index predicted
   1     1
   2     1
   3     0
   4     0
   5     1

And another that has the form:另一个具有以下形式:

index actual
   2    1
   4    0

I want the data frame:我想要数据框:

index predicted actual
   1     1       nan
   2     1       1
   3     0       nan
   4     0       0
   5     1       nan

I've tried pd.concat([df1,df2], on="index", how="left") and pd.merge(df1, df2, axis=1)我试过pd.concat([df1,df2], on="index", how="left")pd.merge(df1, df2, axis=1)

Both give the dataframe:两者都给出 dataframe:

index predicted actual
   1     1       1
   2     1       0
   3     0       nan
   4     0       nan
   5     1       nan

How can I get the data frame I need.我怎样才能得到我需要的数据框。 Also thanks in advance.也在此先感谢。

You can use the pd.merge() setting the parameters left_index = True and right_index = True您可以使用pd.merge()设置参数left_index = Trueright_index = True

import pandas as pd
df1 = pd.DataFrame({'predicted': [1,1,0,0,1]}, index = (1,2,3,4,5))
df2 = pd.DataFrame({'actual': [1,0]}, index = (2,4))

pd.merge(df1, df2, how = 'left', left_index=True, right_index=True)

This will merge the two dataframes on index and produce the intended result required.这将合并索引上的两个数据帧并产生所需的预期结果。

index   predicted   actual
1       1           NaN
2       1           1.0
3       0           NaN
4       0           0.0
5       1           NaN

If you make sure that your index column is actually the df.index , pd.concat should work:如果你确定你的索引列实际上是df.index , pd.concat 应该工作:

import pandas as pd

left = pd.DataFrame({"predicted": [1, 1, 0, 0, 1]}, index=[1, 2, 3, 4, 5])
right = pd.DataFrame({"actual": [1, 0]}, index=[2, 4])

out = pd.concat([left, right], axis=1)
   predicted  actual
1          1     NaN
2          1     1.0
3          0     NaN
4          0     0.0
5          1     NaN

If they're just columns, such as the following:如果它们只是列,例如以下内容:

left = left.reset_index(names="index")
right = right.reset_index(names="index")

then you can use:然后你可以使用:

left.merge(right, on="index", how="left")
   index  predicted  actual
0      1          1     NaN
1      2          1     1.0
2      3          0     NaN
3      4          0     0.0
4      5          1     NaN

Create index as a temporary column then left join using that then set it as index.创建索引作为临时列,然后使用它进行左连接,然后将其设置为索引。

predict_df = pd.DataFrame({'predicted': [1,1,0,0,1]}, index=range(1,6))
actual_df = pd.DataFrame({'actual': [1,0]}, index=[2,4])

pd.merge(
    left=predict_df.reset_index(),
    right=actual_df.reset_index(),
    how='left',
    on='index'
).set_index('index')
      predicted actual
index       
1     1         NaN
2     1         1.0
3     0         NaN
4     0         0.0
5     1         NaN
df1.join(df2)

out:出去:

predicted  actual
1          1     NaN
2          1     1.0
3          0     NaN
4          0     0.0
5          1     NaN

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

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