简体   繁体   English

将两个大熊猫数据框与第一个索引相连吗?

[英]Join two pandas data frames with the indices of the first?

I have two dataframes, df1 : 我有两个数据df1

    column1     column2
0   A           B
1   A           A
2   C           A
3   None        None
4   None        None

and df2 df2

    id          l
40  100005090   A
188 100020985   B

Now I want to join df1 and df2 , but I don't know how to match the indices. 现在我想加入df1df2 ,但是我不知道如何匹配索引。 If I simply do df1.join(df2) , the indices are aligned to df2 . 如果我只是简单地执行df1.join(df2) ,则索引将与df2对齐。 That is, it finds the 40th entry of df2 and that is now the first entry of the dataframe that starts at 40 ( df1 ). 也就是说,它找到df2的第40个条目,并且现在是从40( df1 )开始的数据帧的第一个条目。 How do I tell pandas to align indices to df1 , meaning that the first entry of df2 is actually index 40? 我如何告诉熊猫将索引与df1对齐,这意味着df2的第一个条目实际上是索引40? That is, I would like to get: 也就是说,我想得到:

    id          l    column1     column2
40  100005090   A          A           B
188 100020985   B          A           A
...

You can take a slice of your df that is the same length as df1 , then you can overwrite the index values and then join : 您可以df1df1长度相同的df切片,然后可以覆盖索引值,然后join

In [174]:    
sub = df.iloc[:len(df1)]
sub.index = df1.index
df1.join(sub)

Out[174]:
            id  l column1 column2
40   100005090  A       A       B
188  100020985  B       A       A

If the dfs are the same length then the first line is not needed, you just overwrite the index with the index values from the other df. 如果df的长度相同,则不需要第一行,您只需用其他df的索引值覆盖索引即可。

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

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