简体   繁体   English

根据其他数据框添加特定的列值

[英]Add specific column values based on other Dataframe

I have this first dataFrame 我有第一个数据框

df1:

A         B       C    D
Car               0
Bike              0
Train             0
Plane             0
Other_1  Plane    2
Other_2  Plane    3
Other 3  Plane    4

and this other one: 而另一个:

df2:

A         B       
Car       4 %        
Bike      5 %        
Train     6 %        
Plane     7 %

So I want to get this combination: 所以我想得到这个组合:

df1:

A         B       C    D
Car               0    4 %
Bike              0    5 %
Train             0    6 %
Plane             0    7 %
Other_1  Plane    2    2
Other_2  Plane    3    3
Other 3  Plane    4    4  

Which is the best way to do this? 哪个是最好的方法?

If df and df2 are identically indexed, then you can use: 如果df和df2的索引相同,则可以使用:

df['D'] = df2['B'].combine_first(df['C'])

Output: 输出:

         A      B  C    D
0      Car    NaN  0  4 %
1     Bike    NaN  0  5 %
2    Train    NaN  0  6 %
3    Plane    NaN  0  7 %
4  Other_1  Plane  2    2
5  Other_2  Plane  3    3
6  Other_3  Plane  4    4

If not identically index, then you can use merge on column A: 如果索引不同,则可以在列A上使用merge

df_out = df.merge(df2, on ='A', how='left', suffixes=('','y'))
df_out.assign(D = df_out.By.fillna(df_out.C)).drop('By', axis=1)

or use @piRSquared improved one-liner : 或使用@piRSquared改进的单线

df.drop('D',1).merge(df2.rename(columns={'B':'D'}), how='left',on ='A')

Output: 输出:

         A      B  C    D
0      Car    NaN  0  4 %
1     Bike    NaN  0  5 %
2    Train    NaN  0  6 %
3    Plane    NaN  0  7 %
4  Other_1  Plane  2    2
5  Other_2  Plane  3    3
6  Other_3  Plane  4    4

map

df1.assign(D=df1.A.map(dict(zip(df2.A, df2.B))))

         A      B  C    D
0      Car    NaN  0  4 %
1     Bike    NaN  0  5 %
2    Train    NaN  0  6 %
3    Plane    NaN  0  7 %
4  Other_1  Plane  2  NaN
5  Other_2  Plane  3  NaN
6  Other_3  Plane  4  NaN

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

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