【问题标题】:Summing Data in data frame in distinct categories在不同类别的数据框中汇总数据
【发布时间】:2019-09-23 00:38:38
【问题描述】:

我创建了一个包含数据的 Excel 电子表格,并已传输到 CSV 文件中。我想在每个不同的年份添加每个种族的数据。我试图创建一个数据索引并尝试对每个种族进行总计,但能够保存或包含数据。 我用过df。以及创建“for”循环,以便我可以按种族保存数据,但收到错误消息。原始的 Excel 表包含与特定年份相关的特定节目的每个种族的数据框。我无法对每个种族每年的列进行汇总。

我应该使用 for 或 if 循环来逐步遍历特定年份,我的方法是否正确?

#this is the first method I have tried
import pandas as pd
import numpy as np

from google.colab import files
uploaded = files.upload()
# df = pd.read_csv('/content/drive/My Drive/allTheaterDataV2.csv')

import io
df = pd.read_csv(io.BytesIO(uploaded['allTheaterDataV2.csv']))
# Daset is now stored in a Pandas Dataframe

#create list that contains the specific season that we want to reference
# print(df)

data = pd.DataFrame(allTheaterDataV2)

dataindex = [20082009, 20102011, 20112012, 20122013, 20132014, 20142015]
print(dataindex)


df.loc['total',:] = df.sum(axis=0)

print(df.loc[1:42, ['ASIAM','AFRAM','LAT','CAU','OTH']].sum())

# The second method I have tried is included below
for i in dataindex:
  # create a new data frame that stores the data per year
  hold_ASIAM = df[df.index == i]
  # allows for data for each season to be contained together
  ETHtotalASIAM = df['ASIAM'].sum()
  hold_ASIAM.append(ETHtotalASIAM)
print(hold_ASIAM)

我希望输出给我每年 (20082009) 每个种族(例如:AFRAM)的总数(一些 #),但实际输出是“名称 'allTheaterDataV2' is not defined'

【问题讨论】:

  • 什么是完整的回溯?看起来您从未定义变量 allTheaterDataV2 之前在这里调用它 data = pd.DataFrame(allTheaterDataV2)
  • 你能提供一些示例数据吗?
  • 如何定义变量?

标签: python pandas csv numpy dataframe


【解决方案1】:

这应该可行。

import pandas as pd

df = pd.DataFrame({'ID':['Billy Elliot','next to normal','shrek','guys and dolls',
                         'west side story', 'pal joey'],
                   'Season' : [20082009,20082009,20082009,
                               20082009,20082009,20082009],
                   'AFRAM' : [2,0,4,4,0,1],
                   'ASIAM' : [0,0,1,0,0,0],
                   'CAU' : [48,10,25,24,28,20],
                   'LAT' : [1,0,1,3,18,0],
                   'OTH' : [0,0,0,0,0,0]}) 

print(df)
#    AFRAM  ASIAM  CAU               ID  LAT  OTH    Season
# 0      2      0   48     Billy Elliot    1    0  20082009
# 1      0      0   10   next to normal    0    0  20082009
# 2      4      1   25            shrek    1    0  20082009
# 3      4      0   24   guys and dolls    3    0  20082009
# 4      0      0   28  west side story   18    0  20082009
# 5      1      0   20         pal joey    0    0  20082009

# drop the ID column since it is just a string
df = df.drop(['ID'], axis = 1)

# group by season and add the other columns
df = df.groupby('Season').sum()

print(df)
#             AFRAM  ASIAM  CAU  LAT  OTH
# Season                                 
# 20082009     11      1  155   23    0

【讨论】:

  • 谢谢,这很有帮助,但我不知道为什么,但我得到了 TypeError: 'int' object is unsliceable。
  • 听起来您需要将df['Season'] 列转换为string,然后再转换为datetime。尝试在df['Season'] = pd.to_datetime(df['Season'], format = '%Y%d%m') 之前添加df['Season'] = df['Season'].astype(str)。我还调整了答案以反映我当前的评论。
  • 谢谢,但我仍然无法得到 print(df) 而是得到一个值错误:未转换的数据仍然存在:3. 我搜索了堆栈溢出,但我无法弄清楚这意味着什么.对于上下文,我在 csv 文件中还有除种族以外的其他数据。我是否也必须删除这些数据?
  • 所以问题是我把你的季节数据误认为是格式为 YYYYDDMM 但它实际上是 YYYY 到 YYYY 结合到 YYYY_YYYY。看看我对上一个答案的最后一次编辑应该是什么。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-04
  • 2020-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-22
相关资源
最近更新 更多