简体   繁体   English

蟒蛇熊猫 用数据透视表填充数据框

[英]Python pandas; fill in data frame with pivot_table

I have a large python script, which makes two dataframes A and B, and at the end, I want to fill in dataframe A with the values of dataframe B, and keep the columns of dataframe A, but it is not going well. 我有一个大型的python脚本,它制作了两个数据框A和B,最后,我想用数据框B的值填充数据框A,并保留数据框A的列,但是效果不佳。

Dataframe A is like this 数据框A是这样的

A   B    C    D    
1   ab
2   bc
3   cd

Dataframe B:
A  BB  CC 
1  C   10
2  C   11 
3  D   12

My output must be: 我的输出必须是:

new dataframe 新数据框

A   B    C   D  
1   ab   10
2   bc   11
3   cd       12  

But my output is 但是我的输出是

A   B    C   D  
1   ab   
2   bc   
3   cd        

Why is it not filling in the values of dataframe B? 为什么不填写数据框B的值? My command is 我的命令是

dfnew = dfB.pivot_table(index='A', columns='BB', values='CC').reindex(index=dfA.index, columns=dfA.columns).fillna(dfA)

I think you need set_index by index column of df for align data, fillna or combine_first and last reset_index : 我认为您需要通过dfindex列设置set_index来对齐数据, fillnacombine_first和last reset_index

dfA = pd.DataFrame({'A':[1,2,3], 'B':['ab','bc','cd'], 'C':[np.nan] * 3,'D':[np.nan] * 3})
print (dfA)
   A   B   C   D
0  1  ab NaN NaN
1  2  bc NaN NaN
2  3  cd NaN NaN

dfB = pd.DataFrame({'A':[1,2,3], 'BB':['C','C','D'], 'CC':[10,11,12]})
print (dfB)
   A BB  CC
0  1  C  10
1  2  C  11
2  3  D  12

df = dfB.pivot_table(index='A', columns='BB', values='CC')
print (df)
BB     C     D
A             
1   10.0   NaN
2   11.0   NaN
3    NaN  12.0

dfA = dfA.set_index('A').fillna(df).reset_index()
#dfA = dfA.set_index('A').combine_first(df).reset_index() 
print (dfA)
   A   B     C     D
0  1  ab  10.0   NaN
1  2  bc  11.0   NaN
2  3  cd   NaN  12.0

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

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