user15051990
user15051990

Reputation: 1895

Convert categories to columns in dataframe python

I have a dataframe which contains two columns. One column contains different categories and other contains values.

import pandas as pd

data={"category":["Topic1","Topic2","Topic3","Topic2","Topic1","Topic3"], "value":["hello","hey","hi","name","valuess","python"]}

df=pd.DataFrame(data=data)

I want different categories into column as given below.

Current Input:

category    value
  Topic1    hello
  Topic2      hey
  Topic3       hi
  Topic2     name
  Topic1  valuess
  Topic3   python

Desired Output:

Topic1  Topic2 Topic3
hello    hey    hi
valuess name    python

I tried using transposing the dataframe but not getting the expected result.

Upvotes: 4

Views: 5696

Answers (3)

BENY
BENY

Reputation: 323266

Using pivot and cumcount(For the help key creation)

df.assign(key=df.groupby('category').cumcount()).pivot('key','category','value')
Out[381]: 
category   Topic1 Topic2  Topic3
key                             
0           hello    hey      hi
1         valuess   name  python

Upvotes: 0

Tom Ron
Tom Ron

Reputation: 6181

s = df.groupby('category')['value'].apply(list)
s.apply(pd.Series).T

category   Topic1 Topic2  Topic3
0           hello    hey      hi
1         valuess   name  python

Upvotes: 3

jpp
jpp

Reputation: 164683

You can use pandas.concat along axis=1. This will also work for mismatched lengths.

grouper = df.groupby('category')
df = pd.concat([pd.Series(v['value'].tolist(), name=k) for k, v in grouper], axis=1)

print(df)

    Topic1 Topic2  Topic3
0    hello    hey      hi
1  valuess   name  python

Upvotes: 6

Related Questions