2
我正在使用熊猫在从csv创建的数据框中创建一个新列。熊猫Lambda功能:属性错误'发生在索引0'
[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
我定义一个函数从日期时间字段N(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')
有一些问题与index_col。我以前使用index_col = 1但得到了相同的错误。
当我打印 '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
索引列导致错误。我如何确保这不是功能?
这不是日期时间对象ct,你需要首先转换'df ['dCount'] = pd.to_datetime(df ['dCount'])',然后得到日期'df ['dCount']。dt.date',你可以阅读那些datetime字符串作为日期时间通过传递'parse_dates = ['dCount']'到'read_csv' – EdChum
DfT_raw = pd.read_csv('./ file.csv',parse_dates = ['dCount'],index_col = False)非常感谢!如果您愿意,欢迎随时发表回复 – LearningSlowly