aesir
aesir

Reputation: 565

Reshaping Dataframe without ID variable

I have a pandas dataframe that looks like this:

var | info
id | 1
author | A
title | B
id | 2
author | C
title | D

How can I make it into wide format?

id | author | title
1 | A | B
2 | C | D

I don't have any id variables but each group of 3 observations should constitute a unique row. Is there some way I can generate a unique id number for every 3 observations in the original dataframe? Then I can reshape it easily.

Thanks!

Upvotes: 0

Views: 208

Answers (1)

YS-L
YS-L

Reputation: 14738

You can use the function repeat on Series to duplicate the id values in groups of three rows, and then use pivot to reshape the DataFrame:

import pandas as pd
df = pd.DataFrame({'var': ['id', 'author', 'title', 'id', 'author', 'title'],
                   'info': [1, 'A', 'B', 2, 'C', 'D']})
df['id'] = df['info'].ix[::3].repeat(3).values
df_reshaped = df.pivot(index='id', columns='var', values='info')
# Discard the redundant 'id' column
df_reshaped = df_reshaped[['author', 'title']]
print df_reshaped

Output:

var author title
id              
1        A     B
2        C     D

Upvotes: 1

Related Questions