I have a pandas dateframe and the following code works
df['hour'] = df.index.hour
df['c'] = df['hour'].apply(circadian)
but i was trying to reduce the need to make a 'hour' coloumn, using the following code
df['c'] = df.apply(lambda x: circadian(x.index.hour), axis=1)
but I get the error message
AttributeError: ("'Index' object has no attribute 'hour'", u'occurred at index 2015-09-25 01:00:00')
anyone know how to do this?
Fix error while creating the dataframe To create dataframe we need to use DataFrame(). If we use dataframe it will throw an error because there is no dataframe attribute in pandas. The method is DataFrame(). We need to pass any dictionary as an argument.
pandas. Index is a basic object that stores axis labels for all pandas objects. DataFrame is a two-dimensional data structure, immutable, heterogeneous tabular data structure with labeled axis rows, and columns.
At the very basic level, Pandas objects can be thought of as enhanced versions of NumPy structured arrays in which the rows and columns are identified with labels rather than simple integer indices.
Approach 1:
Convert the DateTimeIndex
to Series
and use apply
.
df['c'] = df.index.to_series().apply(lambda x: circadian(x.hour))
Approach 2:
Use axis=0
which computes along the row-index.
df['c'] = df.apply(lambda x: circadian(x.index.hour), axis=0)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With