熊猫:难以添加新列

时间:2017-08-07 14:20:22

标签: python pandas

我有一个Pandas数据框numeric_df,带有一堆列。我有这个功能:

def textstat_stats(text):
    difficulty = textstat.flesch_reading_ease(text)
    grade_difficulty = textstat.flesch_kincaid_grade(text)
    gfog = textstat.gunning_fog(text)
    smog = textstat.smog_index(text)
    ari = textstat.automated_readability_index(text)
    cli = textstat.coleman_liau_index(text)
    lwf = textstat.linsear_write_formula(text)
    dcrs = textstat.dale_chall_readability_score(text)
    return pd.Series([difficulty, grade_difficulty, gfog, smog, ari, cli, lwf, dcrs])

返回Pandas系列。现在我试着这个:

numeric_df[['difficulty', 'grade_difficulty','gfog','smog','ari','cli','lwf','dcrs']] = textstat_stats(text)

但是,我收到此错误:

KeyError: "['difficulty' 'grade_difficulty' 'gfog' 'smog' 'ari' 'cli' 'lwf' 'dcrs'] not in index"

我做错了什么?

谢谢!

1 个答案:

答案 0 :(得分:0)

您似乎需要向Series添加索引,以创建列名称:

def textstat_stats(text):
    difficulty = textstat.flesch_reading_ease(text)
    grade_difficulty = textstat.flesch_kincaid_grade(text)
    gfog = textstat.gunning_fog(text)
    smog = textstat.smog_index(text)
    ari = textstat.automated_readability_index(text)
    cli = textstat.coleman_liau_index(text)
    lwf = textstat.linsear_write_formula(text)
    dcrs = textstat.dale_chall_readability_score(text)
    idx = ['difficulty', 'grade_difficulty','gfog','smog','ari','cli','lwf','dcrs']
    return pd.Series([difficulty, grade_difficulty, gfog, smog, ari, cli, lwf, dcrs], 
                     index=idx)

df = textstat_stats(text)
print (df)