简体   繁体   English

从另一个数据帧中的列值替换pandas数据帧中的列中的值

[英]Replacing value in a column in pandas dataframe from a column value in another dataframe

I have two dataframes df1 and df2 我有两个数据帧df1df2

s = {'id': [4735,46,2345,8768,807],'city': ['a', 'b', 'd', 'e', 'f']}
s1 = {'id': [4735],'city_in_mail': ['x']}
df1 = pd.DataFrame(s)
df2 = pd.DataFrame(s1)

df1 looks like df1看起来像

     id city
0  4735    a
1    46    b
2  2345    d
3  8768    e
4   807    f

and df2 looks like: df2看起来像:

     id city_in_mail
0  4735            x

I want to replace the value of column city in dataframe df1 from the value of column city_in_mail from dataframe df2 for the row where the id value is same. 我想将数据帧df1中列city的值从数据帧df2的列city_in_mail的值city_in_mailid值相同的行。

So my df1 should become: 所以我的df1应该成为:

     id city
0  4735    x
1    46    b
2  2345    d
3  8768    e
4   807    f 

How can do this with pandas ? 大熊猫怎么做?

Use indexes to match and then loc 使用索引来匹配,然后loc

df1 = df1.set_index('id')
df2 = df2.set_index('id')
df1.loc[df1.index.isin(df2.index), :] = df2.city_in_mail

Or use update 或者使用update

c = df1.city
c.update(df2.city_in_mail)
df1['city'] = c

All outputs 所有输出

        city
id  
4735    x
46      b
2345    d
8768    e
807     f

Of course, feel free to do df1.reset_index() in the end to go back to previous structure. 当然,最后可以自由地做df1.reset_index()以回到之前的结构。

Using merge with .loc 使用与.loc merge

s=df1.merge(df2,how='outer')
s.loc[s.city_in_mail.notnull(),'city']=s.city_in_mail
s
  city    id city_in_mail
0    x  4735            x
1    b    46          NaN
2    d  2345          NaN
3    e  8768          NaN
4    f   807          NaN

Try combine_first with rename to align column index: 尝试使用combine_first rename以对齐列索引:

df2.set_index('id')\
   .rename(columns={'city_in_mail':'city'})\
   .combine_first(df1.set_index('id'))\
   .reset_index()

Output: 输出:

       id city
0  4735.0    x
1    46.0    b
2  2345.0    d
3  8768.0    e
4   807.0    f

Note: You can reassign this back to df1 if you choose. 注意:如果您愿意,可以将其重新分配给df1。

Also .map + .fillna (if 'id' is a unique key in df2 ) 另外.map + .fillna (如果'id'df2的唯一键)

df1['city'] = df1.id.map(df2.set_index('id').city_in_mail).fillna(df1.city)

print(df1)
#     id city
#0  4735    x
#1    46    b
#2  2345    d
#3  8768    e
#4   807    f

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

相关问题 用另一个值替换熊猫数据框列中的几个值 - Replacing few values in a pandas dataframe column with another value 用另一列中的相同行值替换 pandas dataframe 列中的值 - Replacing values in pandas dataframe column with same row value from another column Pandas:将从 DataFrame 中提取的值乘以另一个 DataFrame 中的列值 - Pandas: Multiplying a value extracted from a DataFrame to column values in another DataFrame 使用来自另一个具有条件的数据帧的值更新熊猫数据帧列 - update pandas dataframe column with value from another dataframe with condition 用来自另一个数据帧特定列的值替换来自数据帧特定列的 Nan 值 - Replacing Nan value from specific column of a dataframe with value from specific column of another dataframe 用从数据框中另一列的最大值计算的值替换字符串 - Replacing string with value calculated from the max of another column in a dataframe 熊猫列值从另一个数据框值更新 - pandas column value update from another dataframe value Pandas 中 DataFrame 中的列,值为 0 - Column in DataFrame in Pandas with value 0 如果另一个列中的值是另一个DataFrame中的pandas列? - pandas columns from another DataFrame if value is in another column? Pandas:在 dataframe 中创建列,并通过查看另一个 dataframe 为该列分配值 - Pandas: Create column in dataframe and assign value to the column by looking into another dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM