简体   繁体   English

Python DataFrame:将所选值替换或组合到主DataFrame中

[英]Python DataFrame: replace or combine selected values into main DataFrame

I have two pandas DataFrame as below. 我有两个熊猫DataFrame如下。 It contains strings and np.nan values. 它包含字符串和np.nan值。 df = df =

   A    B    C    D    E    F
0  aaa  abx  fwe  dcs  NaN  gsx
1  bbb  daf  dxs  fsx  NaN  ewe
2  ccc  NaN  NaN  NaN  NaN  dfw
3  ddd  NaN  NaN  asc  NaN  NaN
4  eee  NaN  NaN  cse  NaN  NaN
5  fff  NaN  NaN  wer  xer  NaN

df_result = df_result =

   A    C    E    F
2  sfa  NaN  NaN  wes  
4  web  NaN  NaN  NaN  
5  NaN  wwc  wew  NaN  

What I want is copy entire df_result DataFrame to df DataFrame with corresponding columns and index. 我想要的是将整个df_result DataFrame复制到具有相应列和索引的df DataFrame。 so my output would be = 所以我的输出是=

   A    B    C    D    E    F
0  aaa  abx  fwe  dcs  NaN  gsx
1  bbb  dxs  fsx  fsx  NaN  ewe
2  sfa  NaN  NaN  NaN  NaN  wes
3  wen  NaN  NaN  asc  NaN  NaN
4  web  NaN  NaN  cse  NaN  NaN
5  NaN  NaN  wwc  wer  wew  NaN

So basically I want to copy exact values of df_result to df even thought ther are np.nan values like A:5 (changed from fff to NaN). 所以基本上我想将df_result的精确值复制到df,即使认为这是np.nan值,如A:5(从fff更改为NaN)。 Also, I need to keep the order of columns as it is. 另外,我需要保持列的顺序不变。 Please let me know efficient way to do this. 请让我知道执行此操作的有效方法。 Thank you! 谢谢!

df.update(dfr.fillna('NaN'))
df.replace('NaN',np.nan)
Out[501]: 
     A    B    C    D    E    F
0  aaa  abx  fwe  dcs  NaN  gsx
1  bbb  daf  dxs  fsx  NaN  ewe
2  sfa  NaN  NaN  NaN  NaN  wes
3  ddd  NaN  NaN  asc  NaN  NaN
4  web  NaN  NaN  cse  NaN  NaN
5  NaN  NaN  wwc  wer  wew  NaN

Assuming your columns and indices are setup properly, you can just say. 假设您的列和索引设置正确,您可以说。

df.loc[df_result.index,df_result.columns] = df_result 

example of it working: 工作示例:

import pandas as pd
import numpy as np
df=pd.DataFrame(data=[ [1 for y in range(5)] for i in range(5)] , columns=list(range(5)))
df.loc[0::2,2]=np.nan
print(df)
df2 = pd.DataFrame(data=[ [2 for y in range(3)] for i in range(2)] , columns=list(range(2,5)),index=range(1,3))
df2.loc[:,3] = np.nan
print(df2)
df.loc[df2.index,df2.columns] = df2
print(df)

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

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