[英]Referencing item in ForLoop within Pandas column rename
I have two pandas.DataFrame
's and I wish to rename a column named value
, which exists in both, as the name of the dataframe: 我有两个
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)
This does not work - it fails to update the value
column and messes up the format of the dataframe. 这不起作用 - 它无法更新
value
列并混淆数据帧的格式。
Change: 更改:
for i in names:
i = loader(''+i+'.csv') #load data
To: 至:
for name in names:
i = loader(name + '.csv') #load data
Then do the rename with: 然后使用以下命令重命名:
i.rename(columns={'value':name}, inplace=True)
You need to use eval
: 你需要使用
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)
Output: 输出:
a
0 1
1 2
2 3
3 4
print(b)
Output: 输出:
b
0 5
1 6
2 7
3 9
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.