如何比较两个数据框中特定列的行,并在它们相等时用特定值替换它们?

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

How to compare rows of specific columns of two dataframes and in the event of equalness replace with them with specific values?

问题

抱歉,我无法翻译代码部分。以下是您要求的翻译:

"Unfortunately I couldn't figure out my Python/pandas problem and I couldn't find the answer on google, stackoverflow or chatgpt.."

很不幸,我无法解决我的Python/pandas问题,也无法在Google、Stackoverflow或ChatGPT上找到答案。

"I hope that you'll be willing to help me :)"

我希望你愿意帮助我 如何比较两个数据框中特定列的行,并在它们相等时用特定值替换它们?

"I want to compare the all rows of one column of one data frame (df1) with all rows of one column of another data frame (df2). If the values are equal then the value of the cell of df2 shall be replaced by the value of the cell next to the compared cell of df1."

我想要比较一个数据框(df1)的一个列的所有行与另一个数据框(df2)的一个列的所有行。如果值相等,那么df2的单元格的值应该被df1中比较单元格旁边的单元格的值替换。

"So for example:"

例如:

"I got the following data frames:"

我有以下数据框:

"df1 = pd.DataFrame({'ID': [1, 2, 3], 'VALUES': [4, 6, 7]})"

"df2 = pd.DataFrame({'NAME': [7, 8, 9, 10], 'VALUES': [4, 6, 6, 7]})"

"And the end result should look like this:"

最终结果应该如下所示:

"print(df2)"

"| NAME | VALUES |"
"| ---- | -------- |"
"| 7 | 1 |"
"| 8 | 2 |"
"| 9 | 2 |"
"| 10 | 3 |"

"Would be so awesome if anyone of you could help me out :)"

如果你们中的任何人能帮助我,那就太棒了 :)"

"Thanks in advance!"

提前感谢!"

英文:

Unfortunately I couldn't figure out my Python/pandas problem and I couldn't find the answer on google, stackoverflow or chatgpt..

I hope that you´ll be willing to help me 如何比较两个数据框中特定列的行,并在它们相等时用特定值替换它们?

I want to compare the all rows of one column of one data frame (df1) with all rows of one column of another data frame (df2). If the values are equal then the value of the cell of df2 shall be replaced by the value of the cell next to the compared cell of df1.

So for example:

I got the following data frames:

df1 = pd.DataFrame({'ID': [1, 2, 3], 'VALUES': [4, 6, 7]})

df2 = pd.DataFrame({'NAME': [7, 8, 9, 10], 'VALUES': [4, 6, 6, 7]})

And the end result should look like this:

print(df2)


| NAME | VALUES   |
| ---- | -------- |
| 7    | 1        |
| 8    | 2        |
| 9    | 2        |
| 10   | 3        |

Would be so awesome if anyone of you could help me out 如何比较两个数据框中特定列的行,并在它们相等时用特定值替换它们?

Thanks in advance!

答案1

得分: 0

import pandas as pd
df1 = pd.DataFrame({'ID': [1, 2, 3], 'VALUES': [4, 6, 7]})
df2 = pd.DataFrame({'NAME': [7, 8, 9, 10], 'VALUES': [4, 6, 6, 7]})
df_out = df2.merge(df1, on=['VALUES'], how='left')
df_out['VALUES'] = df_out['ID']
df_out[['NAME','VALUES']]
英文:
import pandas as pd
df1 = pd.DataFrame({'ID': [1, 2, 3], 'VALUES': [4, 6, 7]})
df2 = pd.DataFrame({'NAME': [7, 8, 9, 10], 'VALUES': [4, 6, 6, 7]})
df_out = df2.merge(df1,on=['VALUES'],how='left')
df_out['VALUES'] = df_out['ID']
df_out[['NAME','VALUES']]

Please try this ,
it gives me below result

如何比较两个数据框中特定列的行,并在它们相等时用特定值替换它们?

huangapple
  • 本文由 发表于 2023年6月29日 23:36:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76582578.html
匿名

发表评论

匿名网友

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

确定