I need to write a function which calculates the number of tweets that were posted per day.
Function Specifications:
- It should take a pandas dataframe as input. It should return a new dataframe, grouped by day, with the number of tweets for that day.
- The index of the new dataframe should be named Date, and the column of the new dataframe should be 'Tweets', corresponding to the date and number of tweets, respectively.
- The date should be formated as yyyy-mm-dd, and should be a datetime object.
My code is :
def number_of_tweets_per_day(df):
df = pd.DataFrame(twitter_df['Date','Tweets'], columns =['Date', 'Tweets'])
df['Date'] = pd.to_datetime(df['Date'], format = '%Y%m%d')
df['Tweets'] = df['Tweets'].astype(str)
grouped = pd.df.groupby(['Date'],['Tweets'])
final = pd.df.count()
return final
However when I try to run command 'number_of_tweets_per_day(twitter_df.copy())' I get an error saying twitter_df not defined.
DF:

Expected output:

Attached links to an image of input dataframe and image of expected output.
What am I doing wrong?