# Calculating mean of data frame inside a series object

I have a data frame with following structure:

df.columns

Index(['first_post_date', 'followers_count', 'friends_count', 'last_post_date','min_retweet', 'retweet_count', 'screen_name', 'tweet_count',  'tweet_with_max_retweet', 'tweets', 'uid'],
dtype='object')


Inside the tweets series, each cell is another data frame containing all the tweets of an user.

df.tweets[0].columns

Index(['created_at', 'id', 'retweet_count', 'text'], dtype='object')


I want to perform calculation on the tweets of each users, for example, finding average number of retweets of each user, the tweet with maximum retweets etc.

How can I do these?

• Can you upload a few lines of data so we can present a working solution?
– Emre
Commented May 26, 2017 at 18:35
• @Emre, I loaded the data in data frame using pandas.read_pickle(filename) Commented May 26, 2017 at 19:50

In the absence of a MultiIndex (the Right Way$^\mathrm{TM}$), the apply method can do what you want; e.g.

df.assign(
max_retweet=df.tweets.apply(lambda x: x.retweet_count.argmax('retweet_count')),
avg_retweet=df.tweets.apply(lambda x: x.retweet_count.mean())
)

avg_retweet  max_retweet
26662     0.045476          187
32316     0.821538          427
25879     0.633681          583
43411     0.112465          421
28840     0.472222            8


Also, don't share data with pickle; it's dangerous.

• I have a similar data structure and apply method seems not work for my situation. Can you help me to solve my problem? hackmd.io/s/ryrCfAjZG Commented Dec 11, 2017 at 9:40
• Please post a new question at StackOverflow.
– Emre
Commented Dec 11, 2017 at 18:04