[英]How do I extract column values from one dataframe to another?
I have two pandas data frames df1 and df2我有两个 pandas 数据帧 df1 和 df2
**df1** **df2**
cat id frequency id (other cols) A B C
A 23 2 23 ............. nan nan nan
A 43 8 43 ............. nan nan nan
B 23 56 30 ............. nan nan nan
C 30 4
I am looking for a way on how to extract information form df1 to df2 resulting in the format below, where the values of columns A, B and C are the frequency values from df1我正在寻找一种方法来从 df1 到 df2 中提取信息,从而得到以下格式,其中 A、B 和 C 列的值是 df1 的频率值
**df2**
id (other cols) A B C
30 .......... 0 0 4
23 .......... 2 56 0
43 .......... 8 0 0
Use DataFrame.pivot
with DataFrame.combine_first
:将
DataFrame.pivot
与DataFrame.combine_first
一起使用:
df11 = df1.pivot('cat', 'id', 'frequency')
#if id is column
df = df2.set_index('id').combine_first(df11)
#if id is index
df = df2.combine_first(df11)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.