简体   繁体   English

Python Pandas-使用另一个数据框列的值更新数据框列

[英]Python Pandas-Update a data frame column with values from another

i'm trying get better at Python and decided to do some analysis on one of my passions.我正在努力在 Python 方面变得更好,并决定对我的一个爱好进行一些分析。 Wrestling!摔角! In this case, Japanese Wrestling!在这种情况下,日本摔跤!

Basically I'm trying to update values in one data frame from another data frame.基本上我试图从另一个数据框中更新一个数据框中的值。 Here's what my first data frame looks like这是我的第一个数据框的样子

|   | Wrestler          | Matches | DMR |
| 0 | TETSUYA NAITO     | 9       | 0   |
| 1 | HIROSHI TANAHASHI | 9       | 0   |
| 2 | BAD LUCK FALE     | 9       | 0   |
| 3 | KOTA IBUSHI       | 9       | 0   |
| 4 | ZACK SABRE JR.    | 9       | 0   |
| 5 | HIROOKI GOTO      | 9       | 0   |
| 6 | TOMOHIRO ISHII    | 9       | 0   |
| 7 | TOGI MAKABE       | 9       | 0   |
| 8 | YOSHI-HASHI       | 9       | 0   |
| 9 | YUJI NAGATA       | 9       | 0   |

The column I'm trying to update is DMR*(Dave Meltzer Ratings)* from another data frame that is generated from some data I input:我要更新的列是 DMR*(Dave Meltzer Ratings)* 来自另一个数据框,该数据框是根据我输入的一些数据生成的:

| Wrestler          | DMR      |
| BAD LUCK FALE     | 3.166667 |
| HIROOKI GOTO      | 3.694444 |
| HIROSHI TANAHASHI | 4.111111 |
| KOTA IBUSHI       | 4.222222 |
| TETSUYA NAITO     | 4        |
| TOGI MAKABE       | 3.611111 |
| TOMOHIRO ISHII    | 4.25     |
| YOSHI-HASHI       | 3.638889 |
| YUJI NAGATA       | 4.138889 |
| ZACK SABRE JR.    | 3.611111 |

I have a feeling it's something simple but I couldn't find anything that would explain how to do it.我有一种感觉,这很简单,但我找不到任何可以解释如何去做的事情。 Any help on this would be greatly appreciated.对此的任何帮助将不胜感激。

Thanks, Shan谢谢,山

Use map by Series :Series使用map

df1['DMR'] = df1['Wrestler'].map(df2.set_index('Wrestler')['DMR'])

Or merge with left join and drop for remove column:或与left join mergedrop以删除列:

df1 = pd.merge(df1.drop('DMR', axis=1), df2, how='left')

print (df1)
            Wrestler  Matches       DMR
0      TETSUYA NAITO        9  4.000000
1  HIROSHI TANAHASHI        9  4.111111
2      BAD LUCK FALE        9  3.166667
3        KOTA IBUSHI        9  4.222222
4     ZACK SABRE JR.        9  3.611111
5       HIROOKI GOTO        9  3.694444
6     TOMOHIRO ISHII        9  4.250000
7        TOGI MAKABE        9  3.611111
8        YOSHI-HASHI        9  3.638889
9        YUJI NAGATA        9  4.138889

Notice:注意:

Values in Wrestler column in df2 have to be unique. df2Wrestler列中的df2必须是唯一的。

So I am going to assume your first dataframe is called df1 and your second one is called df2.因此,我将假设您的第一个数据帧称为 df1,而您的第二个数据帧称为 df2。 Then this would work:那么这将起作用:

df1 = df1.drop('DMR', 1)
df = merge(df1, df2, on = 'Wrestler')

Just drop DMR from the first dataframe and then merge them on the Wrestler column.只需从第一个数据框中删除 DMR,然后将它们合并到 Wrestler 列中。

暂无
暂无

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

相关问题 Python Pandas-使用另一个数据库的值更新数据框架,而无需替换现有数据库 - Python Pandas-Update a data frame with values from another, without replacing existing 如何根据 pandas 中的条件匹配从另一个数据帧更新数据帧列值 - How to update the data frame column values from another data frame based a conditional match in pandas 我需要基于列名从一个数据框到另一个数据框的值在 python pandas 中 - I need values from one data frame to another data frame in python pandas based on column name 使用Pandas Python中另一个数据框中的另一列的索引更新数据框中的一列 - Update a column in a data frame with the index of another column in another data frame in pandas python Python Pandas 数据帧 基于另一列的计数值 - Python Pandas Data Frame Count values of one column based on another 使用python从另一个数据框中参考有效的countrycode列更新数据框中的country列中的空值 - Update a null values in countryside column in a data frame with reference to valid countrycode column from another data frame using python Python:根据另一个数据框中的日期范围更新列的值 - Python : Update Values of a Column based on Date Range from another Data Frame 如何根据 Python 中另一个数据框的值更新数据框中列的值? - How do I update the values of a column in a data frame based on the values of another data frame in Python? Python - 替换熊猫数据框列中的值 - Python - replace values in a pandas data frame column 在Pandas / Python中合并数据框中的列值 - Merging column values in a data frame in Pandas / Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM