简体   繁体   English

从Pandas中不同数据框中的另一个匹配列更新数据框中的列值

[英]update a column value in a dataframe from another matching column in different dataframe in Pandas

i have two dataframes 我有两个数据帧

 df
 city   mail
  a    satya
  b    def
  c    akash
  d    satya
  e    abc
  f    xyz
#Another Dataframe d as
 city   mail
 x      satya
 y      def
 z      akash
 u      ash

So now i need to update city in df from updated values in 'd' comparing the mails, if some mail id not found it should remain as it was. 所以现在我需要更新df中的城市来自'd'中比较邮件的更新值,如果找不到某些邮件ID,它应该保持不变。 So it should look like 所以看起来应该是这样的

 df ### o/p should be like
 city   mail
  x    satya
  y    def
  z    akash
  x    satya  #repeated so same value should placed here
  e    abc     # not found so as it was
  f    xyz

I have tried -- 我试过了 -

s = {'mail': ['satya', 'def', 'akash', 'satya', 'abc', 'xyz'],'city': ['a', 'b', 'c', 'd', 'e', 'f']}
s1 = {'mail': ['satya', 'def', 'akash', 'ash'],'city': ['x', 'y', 'z', 'u']}
df = pd.DataFrame(s)
d = pd.DataFrame(s1)
#from google i tried
df.loc[df.mail.isin(d.mail),['city']] = d['city']

#giving erronous result as #giving错误的结果为

 city   mail
 x  satya
 y  def
 z  akash
 u  satya  ###this value should be for city 'x'
 e    abc
 f    xyz

I can't do a merge here on='mail',how='left', as in one dataframe i have less customer.So after merging, how can i map the value of non matching mail's city in merged one. 我不能在='mail',how ='left'进行合并,因为在一个数据帧中我的客户较少。因此,在合并之后,如何在合并后的城市中映射非匹配邮件城市的值。

Please suggest. 请建议。

It looks like you want to update the city value in df from the city value in d . 看起来你要更新的city中值dfcity价值d The update function is based on the index, so this first needs to be set. update功能基于索引,因此首先需要设置。

# Add extra columns to dataframe.
df['mobile_no'] = ['212-555-1111'] * len(df)
df['age'] = [20] * len(df)

# Update city values keyed on `mail`.
new_city = df[['mail', 'city']].set_index('mail')
new_city.update(d.set_index('mail'))
df['city'] = new_city.values

>>> df
  city   mail     mobile_no  age
0    x  satya  212-555-1111   20
1    y    def  212-555-1111   20
2    z  akash  212-555-1111   20
3    x  satya  212-555-1111   20
4    e    abc  212-555-1111   20
5    f    xyz  212-555-1111   20

暂无
暂无

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

相关问题 使用来自另一个具有条件的数据帧的值更新熊猫数据帧列 - update pandas dataframe column with value from another dataframe with condition 如何在匹配来自不同数据帧的另一列的行后更新一列 dF 的值 - How to update the value of one column dF after matching the rows of from another column from different dataframe 如何查找两个 pandas dataframe 并从另一个 dataframe 列更新第一个 dataframe 列中的值? - how to do lookup on two pandas dataframe and update its value in first dataframe column from another dataframe column? 熊猫列值从另一个数据框值更新 - pandas column value update from another dataframe value 如何根据另一个 DataFrame 中的列更新 Pandas DataFrame 中的列 - How to update a column in pandas DataFrame based on column from another DataFrame 从另一个数据帧中的列值替换pandas数据帧中的列中的值 - Replacing value in a column in pandas dataframe from a column value in another dataframe 在列中查找匹配值并创建另一列 pandas dataframe - Find matching value in column and create another column pandas dataframe Pandas:用于匹配行索引 - 使用不同列大小的其他 dataframe 的值更新 dataframe 值 - Pandas: for matching row indices - update dataframe values with values from other dataframe with a different column size 根据来自不同 dataframe 的另一列,将值更新到当前 dataframe 中的新列 - Update the value into new column in current dataframe based on another column from different dataframe 当来自熊猫中另一个数据框的键匹配时更新数据框的列 - Update column of a dataframe when key matches from another dataframe in pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM