我寫了下面的函數到數據幀的數列轉換成數值:大熊貓:錯誤的DataFrame.unstack
def factorizeMany(data, columns):
""" Factorize a bunch of columns in a data frame"""
data[columns] = data[columns].stack().rank(method='dense').unstack()
return data
調用它像這樣
trainDataPre = factorizeMany(trainDataMerged.fillna(0), columns=["char_{0}".format(i) for i in range(1,10)])
給我一個錯誤。我不知道在哪裏尋找原因,可能是錯誤的輸入?
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-14-357f8a4b2ef8> in <module>()
1 #trainDataPre = trainDataMerged.drop(["people_id", "activity_id", "date"], axis=1)
2 #trainDataPre = trainDataMerged.fillna(0)
----> 3 trainDataPre = mininggear.factorizeMany(trainDataMerged.fillna(0), columns=["char_{0}".format(i) for i in range(1,10)])
/Users/cls/Dropbox/Datengräber/Kaggle/RedHat/mininggear.py in factorizeMany(data, columns)
15 def factorizeMany(data, columns):
16 """ Factorize a bunch of columns in a data frame"""
---> 17 data[columns] = data[columns].stack().rank(method='dense').unstack()
18 return data
19
/usr/local/lib/python3.5/site-packages/pandas/core/series.py in unstack(self, level, fill_value)
2041 """
2042 from pandas.core.reshape import unstack
-> 2043 return unstack(self, level, fill_value)
2044
2045 # ----------------------------------------------------------------------
/usr/local/lib/python3.5/site-packages/pandas/core/reshape.py in unstack(obj, level, fill_value)
405 else:
406 unstacker = _Unstacker(obj.values, obj.index, level=level,
--> 407 fill_value=fill_value)
408 return unstacker.get_result()
409
/usr/local/lib/python3.5/site-packages/pandas/core/reshape.py in __init__(self, values, index, level, value_columns, fill_value)
90
91 # when index includes `nan`, need to lift levels/strides by 1
---> 92 self.lift = 1 if -1 in self.index.labels[self.level] else 0
93
94 self.new_index_levels = list(index.levels)
AttributeError: 'Index' object has no attribute 'labels'
你能提供你的'trainDataMerged'數據幀的樣本? –
@ AlbertoGarcia-Raboso發佈巨大的CSV字符串?如果該樣本不包含導致錯誤的數據,該怎麼辦?正如下面的答案所示,這個問題可以通過一些見解來回答。 – clstaudt