从df1和df2创建DataFrame,如果在df1的列”value”中不存在,则从df2中获取空值。

huangapple go评论72阅读模式
英文:

Create DataFrame from df1 and df2 and take empty value from df2 for column value if not exist in df1 column value

问题

需要从df2中提取如果在df1中缺失的frn,并创建一个新的DataFrame。

英文:
df1 = pd.DataFrame({'call_sign': ['ASD','BSD','CDSF','GFDFD','FHHH'],'frn':['123','124','','656','']})

df2 = pd.DataFrame({'call_sign': ['ASD','CDSF','BSD','GFDFD','FHHH'],'frn':['1234','','124','','765']})

need to get a new df like

df2 = pd.DataFrame({'call_sign': ['ASD','BSD','CDSF','GFDFD','FHHH'],'frn':['123','','124','656','765']})

I need to take frn from df2 if it's missing in df1 and create a new df

答案1

得分: 1

将空字符串替换为缺失值,并使用 DataFrame.set_indexDataFrame.fillna,因为需要按照 df2.call_sign 的顺序添加 DataFrame.reindex:

df = (df1.set_index('call_sign').replace('', np.nan)
          .fillna(df2.set_index('call_sign').replace('', np.nan))
          .reindex(df2['call_sign']).reset_index())
print(df)
  call_sign  frn
0       ASD  123
1      CDSF  NaN
2       BSD  124
3     GFDFD  656
4      FHHH  765
英文:

Replace empty strings to missing values and use DataFrame.set_index with DataFrame.fillna, because need ordering like df2.call_sign add DataFrame.reindex:

df = (df1.set_index('call_sign').replace('', np.nan)
          .fillna(df2.set_index('call_sign').replace('', np.nan))
         .reindex(df2['call_sign']).reset_index())
print(df)
  call_sign  frn
0       ASD  123
1      CDSF  NaN
2       BSD  124
3     GFDFD  656
4      FHHH  765

答案2

得分: 0

如果您要更新df2,可以使用布尔索引

# 是否为空字符串?
m = df2['frn'].eq('')
# 从df1的值更新这些行
df2.loc[m, 'frn'] = df2.loc[m, 'call_sign'].map(df1.set_index('call_sign')['frn'])

更新后的df2

  call_sign   frn
0       ASD  1234
1      CDSF      
2       BSD   124
3     GFDFD   656
4      FHHH   765
英文:

If you want to update df2 you can use boolean indexing:

# is frn empty string?
m = df2['frn'].eq('')
# update those rows from the value in df1
df2.loc[m, 'frn'] = df2.loc[m, 'call_sign'].map(df1.set_index('call_sign')['frn'])

Updated df2:

  call_sign   frn
0       ASD  1234
1      CDSF      
2       BSD   124
3     GFDFD   656
4      FHHH   765

答案3

得分: 0

temp = df1.merge(df2, how='left', on='call_sign')
df1['frn'] = temp['frn_x'].where(temp['frn_x'] != '', temp['frn_y'])
    call_sign    frn
0  ASD         123
1  BSD         
2  CDSF        124
3  GFDFD       656
4  FHHH        765
英文:
temp = df1.merge(df2,how='left',on='call_sign')
df1['frn']=temp.frn_x.where(temp.frn_x!='',temp.frn_y)
    call_sign 	frn
0 	ASD 	    123
1 	BSD 	
2 	CDSF 	    124
3 	GFDFD 	    656
4 	FHHH 	    765

huangapple
  • 本文由 发表于 2023年2月16日 14:18:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75468512.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定