Python 如何将数据帧作为arg传递并操作它?
我编写了一个函数来获取一个数据帧并生成一个数据字典,以供以后处理。这看起来很简单,但我得到了一个错误:Python 如何将数据帧作为arg传递并操作它?,python,pandas,Python,Pandas,我编写了一个函数来获取一个数据帧并生成一个数据字典,以供以后处理。这看起来很简单,但我得到了一个错误: AttributeError: 'Index' object has no attribute 'value' def createDataDict(df, keyname): """ create a dictionary of dictionaries that looks like this: { <License_Number> : {<label
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
AttributeError:“Index”对象没有属性“value”
def createDataDict(df,键名):
“”“创建一个字典字典,如下所示:
{ : {:, :, ...}
例如:
{123456:{'violator':错误,总销售额:1000,'violation'count':2},…}
"""
“”“对于数据帧中的每一行,提取密钥的许可证号,
获取每个列的名称和值并添加到字典中
"""
keys=df[keyname]
对于键入键:
dict={}
对于反转的列(列表(df.columns.value)):
特征值={}
特征值[col]=df[col]
dict[key]=特征值
您可以尝试df.columns
而不是您编写的df.columns.value
还有几件事需要注意
颠倒的是没有意义的
OrderedDict
dict(df[keyname])
(我会调用keyname
keynames
,但这就是我。您可以随意调用它。)因为您确实有一个类型错误。您应该访问
df.columns.values
而不是df.columns.value