简体   繁体   English

大熊猫-如果同一行的其他值出现在第二个数据框中,则替换列的值

[英]Pandas - replace values of a column if other values from same row appear in second data frame

Input are two dataframes. 输入是两个数据框。 Below are short versions of them with only a few rows. 下面是它们的简短版本,仅有几行。

df1
+-----+------+------+
| No  | Type | Desc |
+-----+------+------+
| 123 | A    | Bla  |
| 123 | B    | Bla  |
| 123 | D    | Bla  |
| 342 | A    | Bla  |
| 342 | C    | Bla  |
| 543 | B    | Bla  |
| 543 | C    | Bla  |
+-----+------+------+

df2
+-----+------+------+
| No  | Type | Desc |
+-----+------+------+
| 123 | A    | Lala |
| 342 | A    | Lala |
| 342 | C    | Lala |
+-----+------+------+

Both data frames have more than the columns above, but the others do not matter in this case. 这两个数据框都比上面的列多,但是在这种情况下其他两个都没有关系。

I would like to change values of column Desc to Done for rows of df1 in case this row (meaning No and Type ) also appear in df2 . 我想为df1行更改Desc to Done列的值,以防该行(表示NoType )也出现在df2

df1
+-----+------+------+
| No  | Type | Desc |
+-----+------+------+
| 123 | A    | Done |
| 123 | B    | Bla  |
| 123 | D    | Bla  |
| 342 | A    | Done |
| 342 | C    | Done |
| 543 | B    | Bla  |
| 543 | C    | Bla  |
+-----+------+------+

Thank you :) 谢谢 :)

Use merge with numpy.where : 使用与numpy.where merge

df3 = df1[['No','Type']].merge(df2, on=['No','Type'], how='left')
df3['Desc'] = np.where(df3['Desc'].notnull(), 'Done', df1['Desc'])
print (df3)
    No Type  Desc
0  123    A  Done
1  123    B   Bla
2  123    D   Bla
3  342    A  Done
4  342    C  Done
5  543    B   Bla
6  543    C   Bla

You can find the rows of df1 , that are present in df2 with a left merge and then change the Desc to Done . 您可以通过左合并找到df2中存在的df1行,然后将Desc更改为Done

mer = df1.merge(df2, on=['No', 'Type'], how='left')
mer.loc[mer['Desc_y'].notnull(), 'Desc_x'] = 'Done'
df1['Desc'] = mer['Desc_x']

Output: 输出:

    No  Type Desc
0   123 A   Done
1   123 B   Bla
2   123 D   Bla
3   342 A   Done
4   342 C   Done
5   543 B   Bla
6   543 C   Bla

暂无
暂无

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

相关问题 熊猫-用同一列中的其他值替换列中的值 - Pandas - Replace values in column with other values from the same column 如何在特定的熊猫数据框列中查找值,然后将该行中的其他值存储在单独的变量中 - How to look for a value in a specific pandas data frame column and then store the other values from that row in separate variables 如何替换熊猫数据框列中的值? - How to replace values in a pandas data frame column? Python - 替换熊猫数据框列中的值 - Python - replace values in a pandas data frame column 如果满足基于同一数据帧中其他2列的行值的条件,则在数据帧的列行中填充值 - Filling values in rows of column in a data frame, if condition based on 2 other columns row values in the same data frame is met 根据Pandas中第二列的条件,用另一行的同一列的值填充特定行的列中的值 - Fill values in a column of a particular row with the value of same column from another row based on a condition on second column in Pandas 根据 pandas 数据帧中的其他列值组合列值 - Combine column values based on the other column values in pandas data frame 检查 Pandas 数据框列中的唯一值并与第二列交叉引用 - Checking for unique values in Pandas Data frame column and crossreferenceing with a second column Pandas 根据同一数据框中另一列的条件替换列值 - Pandas Replace column values based on condition upon another column in the same data frame 更改第二行的值(熊猫数据框) - Changing the values of every second row (pandas data frame)
相关标签
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM