Run*_*ean 6 python apply pandas
我有一个pandas日期框架,以下代码可以正常工作
df['hour'] = df.index.hour
df['c'] = df['hour'].apply(circadian)
Run Code Online (Sandbox Code Playgroud)
但我试图使用以下代码减少制作'小时'coloumn的需要
df['c'] = df.apply(lambda x: circadian(x.index.hour), axis=1)
Run Code Online (Sandbox Code Playgroud)
但我收到错误信息
AttributeError: ("'Index' object has no attribute 'hour'", u'occurred at index 2015-09-25 01:00:00')
Run Code Online (Sandbox Code Playgroud)
有人知道怎么做吗?
方法1:
转换DateTimeIndex为Series并使用apply.
df['c'] = df.index.to_series().apply(lambda x: circadian(x.hour))
Run Code Online (Sandbox Code Playgroud)
方法2:
用于axis=0沿行索引计算.
df['c'] = df.apply(lambda x: circadian(x.index.hour), axis=0)
Run Code Online (Sandbox Code Playgroud)