Python 3.x 如何添加具有相同id的数据帧?
我是数据科学学习的初学者。浏览了熊猫主题,我在这里发现了一个任务,我无法理解哪里出了问题。让我解释一下这个问题 我有三个数据帧:Python 3.x 如何添加具有相同id的数据帧?,python-3.x,pandas,Python 3.x,Pandas,我是数据科学学习的初学者。浏览了熊猫主题,我在这里发现了一个任务,我无法理解哪里出了问题。让我解释一下这个问题 我有三个数据帧: gold = pd.DataFrame({'Country': ['USA', 'France', 'Russia'], 'Medals': [15, 13, 9]} ) silver = pd.DataFrame({'Country': ['USA', 'Germany', '
gold = pd.DataFrame({'Country': ['USA', 'France', 'Russia'],
'Medals': [15, 13, 9]}
)
silver = pd.DataFrame({'Country': ['USA', 'Germany', 'Russia'],
'Medals': [29, 20, 16]}
)
bronze = pd.DataFrame({'Country': ['France', 'USA', 'UK'],
'Medals': [40, 28, 27]}
)
在这里,我需要把所有的奖牌加在一列,国家在另一列。当我添加时,它正在显示NAN。所以,我用零值填充NAN,但仍然无法得到应有的输出
代码:
实际产量:
Medals
Country
France NaN
Germany NaN
Russia NaN
UK NaN
USA 72.0
预期:
Medals
Country
USA 72.0
France 53.0
UK 27.0
Russia 25.0
Germany 20.0
告诉我有什么问题。只需使用
groupby
sum
pd.concat([gold,silver,bronze]).groupby('Country').sum()
Out[1306]:
Medals
Country
France 53
Germany 20
Russia 25
UK 27
USA 72
修复代码
silver.add(gold,fill_value = 0).add(bronze,fill_value=0)
如果我们期望浮点数:
pd.concat([gold,silver,bronze]).groupby('Country').sum().astype(float)
pd.concat([gold,silver,bronze]).groupby('Country').sum().astype(float)
# For a video solution of the code, copy-paste the following link on your browser:
# https://youtu.be/p0cnApQDotA
import numpy as np
import pandas as pd
# Defining the three dataframes indicating the gold, silver, and bronze medal counts
# of different countries
gold = pd.DataFrame({'Country': ['USA', 'France', 'Russia'],
'Medals': [15, 13, 9]}
)
silver = pd.DataFrame({'Country': ['USA', 'Germany', 'Russia'],
'Medals': [29, 20, 16]}
)
bronze = pd.DataFrame({'Country': ['France', 'USA', 'UK'],
'Medals': [40, 28, 27]}
)
# Set the index of the dataframes to 'Country' so that you can get the countrywise
# medal count
gold.set_index('Country', inplace = True)
silver.set_index('Country', inplace = True)
bronze.set_index('Country', inplace = True)
# Add the three dataframes and set the fill_value argument to zero to avoid getting
# NaN values
total = gold.add(silver, fill_value = 0).add(bronze, fill_value = 0)
# Sort the resultant dataframe in a descending order
total = total.sort_values(by = 'Medals', ascending = False)
# Print the sorted dataframe
print(total)