我正在尝试从“印度国家GDP数据文件”中选择两个特定的对象,转置数据集(以创建行),并将所有文件中的这些行连接起来以创建新的数据框。我收到此错误。请告诉我如何解决此问题。
import pandas as pd
import numpy as np
import glob
path = r'D:\Data Science\assignment\GDP Assignment\Data IB'
all_files = glob.glob(path+'/*.csv')
li = []
for filename in all_files:
df = pd.read_csv(filename, index_col = None, header = 0)
df = df[['Item','2014-15']]
df = df.set_index('Item')
df = df.T
# Taking the header row
new_header = df.iloc[0] #grab the first row for the header
df = df[1:] #take the data less the header row
# Assign the new header
df.columns = new_header
# Add the section name from the filename
df.loc[:,'States'] = filename.split('\\')[-1].split('.')[0].lstrip('NAD').lstrip('-')
li.append(df)
# Creating a final DF by joining all the df's in the list li
frame = pd.concat(li, axis=0, ignore_index=True)