我有以下数据集:
我想使用熊猫对cantidad
(金额)列按nombre
(名称)分组求和,因此我尝试了:
由于“ Ana”是一个通用名称,因此第一行感到惊讶,因此我检查了以下内容:
好的,所以...“ Ana”的总和是434,而不是1。这是怎么回事?我究竟做错了什么?
最佳答案
您需要skipinitialspace=True
,因为列nombre
中的值具有尾随空格-因此'Ana'
,' Ana'
,' Ana '
...分别分组:
historical_names = pd.read_csv('nombres-1920-1924.csv', skipinitialspace =True)
print (historical_names.head())
resume = historical_names.groupby('nombre')['cantidad'].sum()
print (resume['Ana'])
437
a = historical_names.loc[historical_names['nombre'] == 'Ana', 'cantidad']
print (a)
5 113
10340 138
18776 1
23114 183
26523 2
Name: cantidad, dtype: int64
a = historical_names.loc[historical_names['nombre'] == 'Ana', 'cantidad'].sum()
print (a)
437
historical_names = pd.read_csv('nombres-1920-1924.csv')
print (historical_names.head())
historical_names['nombre'] = historical_names['nombre'].str.strip()
resume = historical_names.groupby('nombre')['cantidad'].sum()
print (resume['Ana'])
437
historical_names = pd.read_csv('nombres-1920-1924.csv')
print (historical_names.head())
resume = historical_names.groupby('nombre')['cantidad'].sum()
print (resume['Ana'])
434
a = historical_names.loc[historical_names['nombre'] == 'Ana', 'cantidad']
print (a)
5 113
10340 138
23114 183
Name: cantidad, dtype: int64
a = historical_names.loc[historical_names['nombre'] == 'Ana', 'cantidad'].sum()
print (a)
434
关于python - 总和和groupby不适用于我使用 Pandas ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46035025/