For循环在多个熊猫数据框中添加列

时间:2020-10-16 03:16:36

标签: python for-loop

晚上好, 首先,我说我处于编码之旅的初期。目前在熊猫项目中使用政府数据中的许多excel表格。每张纸代表一年。在尝试连接整个列表之前,我尝试在每个数据帧中添加一列,以便我知道数据在哪一年来自每个点。当前,代码如下:

'''

df10 = pd.read_excel(r'C:\Market research\national_m2010_dl.xls')
df11 = pd.read_excel(r'C:\Market research\national_m2011_dl.xls')
df12 = pd.read_excel(r'C:\Market research\national_m2012_dl.xls')
df13 = pd.read_excel(r'C:\Market research\national_m2013_dl.xls')
df14 = pd.read_excel(r'C:\Market research\national_m2014_dl.xlsx')
df15 = pd.read_excel(r'C:\Market research\national_m2015_dl.xlsx')
df16 = pd.read_excel(r'C:\Market research\national_m2016_dl.xlsx')
df17 = pd.read_excel(r'C:\Market research\national_m2017_dl.xlsx')
df18 = pd.read_excel(r'C:\Market research\national_m2018_dl.xlsx')
df19 = pd.read_excel(r'C:\Market research\national_m2019_dl.xlsx')
df10['Year'] = '2010'
df11['Year'] = '2011'
df12['Year'] = '2012'
df13['Year'] = '2013'
df14['Year'] = '2014'
df15['Year'] = '2015'
df16['Year'] = '2016'
df17['Year'] = '2017'
df18['Year'] = '2018'
df19['Year'] = '2019'

'''

但是,我敢肯定有一种更干净的方法可以做到这一点,并且想知道是否有更好的方法。我最初尝试了类似于以下的For循环:

'''

for num in range(10,20):
  df+str(num)['Year'] = '20'+str(num)

'''

但是我没有运气。有想法吗?

2 个答案:

答案 0 :(得分:0)

尝试一下:

df_dic = dict()

for n in range(10,20): #remember, the second number in range is exclusive
    year = f"20{n}"
    df = pd.read_excel(f'C:\Market research\national_m{year}_dl.xls')
    df["Year"] = year
    df_dic[year] = df


    

答案 1 :(得分:0)

代替使用df10,df11,...,您可以使用df [10],df [11],...,这使代码非常容易。

for num in range(10,20):
  df[num] = pd.read_excel(r'C:\Market research\national_m20'+str(num)+'_dl.xlsx')
  df[num]['Year'] = '20'+str(num)