英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论