Glen Veigas
Glen Veigas

Reputation: 194

I get 'TypeError:: 'type' object is not iterable' when I use pandas.groupby

I have a csv file containing values of user's social media activity for 20 days I want to get the details of the user activity on Day 1 I did get it by using this piece of code

df['d'] = pd.to_datetime(df['date_time'], format='(%Y,%m,%d,%H,%M,%S)')

day1 = df['d'].dt.date[0]
df = df[df['d'].dt.date.eq(day1)] 

df = df.melt(['date_time','d']) 

df = df[df['value'].eq('Y')] 


d = df.groupby('variable')['date_time'].agg(list).to_dict()

for x,y in d.items():
   print(x,y) 

when I print it this what I get on Google Colab notebook

Instagram [Timestamp('2020-08-23 04:19:05.637617'), Timestamp('2020-08-23 04:20:07.351783'), Timestamp('2020-08-23 04:21:09.069061')]
Facebook [Timestamp('2020-08-23 04:44:49.635657'), Timestamp('2020-08-23 04:45:51.402162'), Timestamp('2020-08-23 05:01:18.989306')]

Now when I run the same on my terminal using after saving it as python file I get this error

Traceback (most recent call last):
  File "example.py", line 43, in <module>
    d=df.groupby('variable')['date_time'].agg(list).to_dict()

TypeError: 'type' object is not iterable

Upvotes: 1

Views: 1131

Answers (1)

jezrael
jezrael

Reputation: 862511

EDIT:

There was problem with pandas version, so for pandas 0.22 need change:

d=df.groupby('variable')['date_time'].agg(list).to_dict()

to:

d=df.groupby('variable')['date_time'].apply(list).to_dict()

Upvotes: 2

Related Questions