我已经创建了一个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)每个种族(例如非洲)的总数(一些#),但实际输出是“name‘allTheaterDataV2’is not defined”
这应该管用。你知道吗
相关问题 更多 >
编程相关推荐