我有一個熊貓數據框,我想在其中更新基於數據框中另一列的列的值。我之前使用以下代碼進行更新:熊貓:更高效的方式來更新熊貓數據框中的一列沒有for循環
for i1, col1 in dfMod.iterrows():
if col1['day'] == "MONDAY":
dfMod.ix[i1,'weekIndex'] = 1
elif col1['day'] == "TUESDAY":
dfMod.ix[i1,'weekIndex'] = 2
elif col1['day'] == "WEDNESDAY":
dfMod.ix[i1,'weekIndex'] = 3
elif col1['day'] == "THURSDAY":
dfMod.ix[i1,'weekIndex'] = 4
elif col1['day'] == "FRIDAY":
dfMod.ix[i1,'weekIndex'] = 5
elif col1['day'] == "SATURDAY":
dfMod.ix[i1,'weekIndex'] = 6
else:
dfMod.ix[i1,'weekIndex'] = 7
但是,數據幀有300,000行,需要永久編譯。有沒有更好的方法來更新列?
看那系列'map'方法。 – BrenBarn
我剛剛也問過這個。我的問題可能對你有用:http://stackoverflow.com/questions/42972081/updating-columns-in-dataframe-using-a-series – sdasdadas