I have a pandas data frame with 'Datetime' column containing timestamp information, I want to extract the hour information from the 'Datetime' column and add it to the hour column of the data frame.
I am confused since my code works if I write the lambda function without the braces
df['Datetime'].apply(lambda x: x.hour)
but, when I try this code instead
df['Datetime'].apply(lambda x: x.hour**()**)
I get the error "TypeError: 'int' object is not callable".
On the other hand when I use split function with lambda expression, it works completely fine with the braces -
df['Reasons'] = df['title'].apply(lambda x: x.split(':')[0])
df['Datetime'].dt.hour. You don't need to dolambda x:for everything. It is often better to just used the pandas method without it. Same for the split. You should do:df['title'].str.split(':').str[0])This is fundamental to writing good code with pandas, so it will greatly benefit you if you can keep these tips in mind.