[英]How can I manipulate python list and convert it to pandas dataframe?
[英]How can I pass a pandas dataframe as an arg and manipulate it?
我編寫了一個函數來獲取數據框並生成數據字典以供以后處理。 似乎很簡單,但是我得到了錯誤:
AttributeError: 'Index' object has no attribute 'value'
def createDataDict(df, keyname):
""" create a dictionary of dictionaries that looks like this:
{ <License_Number> : {<label>:<labelValue>, <feature1>:<feature1Value>, ...}
for example:
{ 123456: {'violator': False, Total_Sales': 1000, 'violation_count': 2} , ...}
"""
""" for each row in dataframe, pull off license number for key,
take each column name and value and add to dictionary
"""
keys = df[keyname]
for key in keys:
dict = {}
for col in reversed(list(df.columns.value)):
feature_values = {}
feature_values[col] = df[col]
dict[key] = feature_values
您可以嘗試使用df.columns
而不是您編寫的df.columns.value
。
還有兩件事要注意...
reversed
操作是沒有意義的。 OrderedDict
。 dict(df[keyname])
。 (我會稱呼keyname
keynames
,但這是必需的。您可以隨意命名。) 因為您確實有類型錯誤。 您應該訪問df.columns.values
而不是df.columns.value
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.