Pandas Lambda 函数:属性错误“发生在索引 0”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33120959/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pandas Lambda Function : attribute error 'occurred at index 0'
提问by LearningSlowly
I am using Pandas to create a new column in a data frame created from a csv.
我正在使用 Pandas 在从 csv 创建的数据框中创建一个新列。
[in] DfT_raw = pd.read_csv('./file.csv', index_col = False)
[in] print(DfT_raw)
[out] Region Name dCount ONS CP S Ref E S Ref N Road \
0 East Midlands E06000015 14/04/00 00:00 37288 434400 336000 A516
1 East Midlands E06000015 14/04/00 00:00 37288 434400 336000 A516
2 East Midlands E06000015 14/04/00 00:00 37288 434400 336000 A516
3 East Midlands E06000015 14/04/00 00:00 37288 434400 336000 A516
I define a function to strip the time from the datetime fieldn (dCount) and then create a new column 'date'
我定义了一个函数来从日期时间字段 (dCount) 中去除时间,然后创建一个新列“日期”
[in] def date_convert(dCount):
return dCount.date()
DfT_raw['date'] = DfT_raw.apply(lambda row: date_convert(row['dCount']), axis=1)
[out] AttributeError: ("'str' object has no attribute 'date'", u'occurred at index 0')
There is some issue with the index_col. I previously used index_col = 1 but got the same error.
index_col 存在一些问题。我以前使用过 index_col = 1 但得到了同样的错误。
When I print 'dCount' I get
当我打印 'dCount' 我得到
0 14/04/00 00:00
1 14/04/00 00:00
2 14/04/00 00:00
3 14/04/00 00:00
4 14/04/00 00:00
The index column is causing the error. How do I ensure this isn't given to the function?
索引列导致错误。我如何确保这不会提供给函数?
回答by EdChum
Your error here is that your dates are strnot datetime, either convert using to_datetime:
你在这里的错误是你的日期str不是datetime,要么使用转换to_datetime:
df['dCount'] = pd.to_datetime(df['dCount'])
or better just tell read_csvto parse that column as datetime:
或者更好地告诉read_csv将该列解析为日期时间:
DfT_raw = pd.read_csv('./file.csv', parse_dates=['dCount'],index_col = False)
Afterwards you can then get just the date by calling the dt.dateaccessor
之后,您可以通过调用dt.date访问器来获取日期

