Python Pandas - 迭代并向空白列添加数据

时间:2017-11-13 11:34:13

标签: python pandas

我正在尝试遍历数据框,对每一行进行分类,并将输出添加到新列的行尾。

似乎是为每一行添加相同的分类

dfMach = pd.read_csv("C:/Users/nicholas/Desktop/machineSum.csv", encoding='latin-1')
dfNew = dfMach
dfNew["Classification"] = ""

for index, row in dfMach.iterrows():
    aVar = dfMach['Summary'].iat[0]
    aClass = cl.classify(aVar)
    dfNew['Classification'] = aClass

我哪里错了?

谢谢

1 个答案:

答案 0 :(得分:5)

使用apply代替明确循环,即

 dfMach['Classification'] = dfMach['Summary'].apply(cl.classify)

在您的代码中需要纠正一些简单的错误,并进行一些改进,即

dfNew = dfMach.copy() # dfNew = dfMach This will not let you create a new copy so you have to use dfMach.copy()

dfNew["Classification"] = ""

for index, row in dfMach.iterrows(): 
    # As @jez suggested we need to use loc for assignemnt 
    dfNew.loc[index, 'Classification'] = cl.classify(row['Summary'])