【发布时间】: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