我有两个pandas.DataFrame
,我希望将名为value
的列重命名为两个,作为数据帧的名称:
#Debt level relative to currency strength
#preamble
import pandas as pd
import statsmodels.formula.api as sm #check that this is actually used
import os
import numpy as np
os.chdir('C:\\Users\\pineapple\\Desktop')
#function construction
def loader(y):
return pd.read_csv(y, header='infer', encoding="ISO-8859+-1")
def viewer(x):
print(x.ix[:])
def delrow(x,y):
return x[pd.notnull(x[y])]
#
names = ['currency', 'debt_ratio']
for i in names:
i = loader(''+i+'.csv') #load data
i = i.replace('..', np.NaN)
for x in range(2007,2015):
y = str(x)
print(y)
i=delrow(i, '' + y + ' [YR'+y+']') #deletes missing values
i.rename(columns = {'' + y + ' [YR'+y+']': ''+ y +''}, inplace=True) #rename columns
i = i.drop(['Series Name','Series Code', 'Country Name', \
'1990 [YR1990]', '2000 [YR2000]' ], axis = 1)
i = pd.melt(i, id_vars=['Country Code'],value_vars=['2007','2008','2009','2010','2011', \
'2012','2013','2014']) #reshape
i.rename(columns={'variable':'year', 'Country Code':'code'}, inplace=True)
viewer(i)
eval(i).rename(columns={'value':i}, inplace=True) #breaks here
i['id'] = i['code'] + i['year']
#output
viewer(i)
这不起作用 - 它无法更新value
列并弄乱了数据框的格式。
答案 0 :(得分:2)
变化:
for i in names:
i = loader(''+i+'.csv') #load data
要:
for name in names:
i = loader(name + '.csv') #load data
然后使用以下命令重命名:
i.rename(columns={'value':name}, inplace=True)
答案 1 :(得分:1)
您需要使用eval
:
a = pd.DataFrame({'Value':[1,2,3,4]})
b = pd.DataFrame({'Value':[5,6,7,9]})
names = ['a','b']
for i in names:
eval(i).rename(columns={'Value':i}, inplace=True)
print(a)
输出:
a
0 1
1 2
2 3
3 4
print(b)
输出:
b
0 5
1 6
2 7
3 9