iraciv94
iraciv94

Reputation: 840

Pivot rows to columns by date pandas

I have a Pandas DataFrame called df that looks like this:

Date           String

2016-08-01      a
2016-08-01      b
2016-08-01      c
2016-06-30      d
2016-06-30      e
2016-06-30      f

And I am trying to obtain:

Date           Column1      Column2        Column3

2016-08-01       a             b              c
2016-06-30       d             e              f

I tried using:

df = pd.pivot_table(df, index='Date')

or:

df.pivot_table(index=['Date'], values="News")

but I keep receiving:

pandas.core.base.DataError: No numeric types to aggregate

What should I do?

Upvotes: 3

Views: 1441

Answers (2)

Erfan
Erfan

Reputation: 42886

Another way to do this is with groupy, apply(list) and after that converting the list values to seperate columns with Series.values.tolist()

# Groupby and get the values in a list per unique value of the Date column
df = df.groupby('Date').String.apply(list).reset_index()

    Date        String
0   2016-06-30  [d, e, f]
1   2016-08-01  [a, b, c]

# Convert the values from the list to seperate columns and after that drop the String column
df[['Column1', 'Column2', 'Column3']] = pd.DataFrame(df.String.values.tolist(), index=df.index)
df.drop('String', axis=1, inplace=True)


    Date        Column1 Column2 Column3
0   2016-06-30  d       e       f
1   2016-08-01  a       b       c

Upvotes: 1

cs95
cs95

Reputation: 402363

Use groupby and cumcount to get repeating counts for date, then use pivot:

(df.assign(Count=df.groupby('Date').cumcount()+1)
   .pivot('Date', 'Count', 'String')
   .add_prefix('Column'))

Count      Column1 Column2 Column3
Date                              
2016-06-30       d       e       f
2016-08-01       a       b       c

Or, set_index and unstack:

(df.set_index(['Date', df.groupby('Date').cumcount()+1])['String']
   .unstack()
   .add_prefix('Column'))

           Column1 Column2 Column3
Date                              
2016-06-30       d       e       f
2016-08-01       a       b       c

Upvotes: 7

Related Questions