我有一个pandas DataFrame,我正在尝试更改列的名称。在我更改名称之前,列是Series(应该是),但在我更改名称后,它们变为DataFrames,因此无法分组等。给我提出问题的代码行是:
df.rename(columns = varDict, inplace = True)
我已经确认:
关于我哪里出错的任何想法?我在Mac OS X上使用python 2.7.5和pandas 0.12.0。提前感谢。完整的代码如下。
import pandas as pd
def FileToDict(filename):
with open(filename, 'rU') as f:
l = [line[:-1] for line in f]
return {x.partition(',')[0]:x.partition(',')[2] for x in l}
#---Preparing the dataset---
df = pd.DataFrame.from_csv('movieDataset.csv')
df = df.set_index(u'row')
#These dictionaries incorporate information for other files
varDict = FileToDict('varNum-to-varName.csv')
titleDict = FileToDict('titleNum-to-titleName.csv')
#Changes the datafile numbers into movie titles
df.datafile = df.datafile.apply(lambda x: titleDict[str(x)])
#Changes the variable numbers into recognizable names
varDict['row'] = 'row'
varDict['datafile'] = 'MovieTitle'
df.rename(columns = lambda x: varDict[str(x)], inplace = True)
#----------------
#At this point the columns become DataFrames rather than Series
#----------------