Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 570
user15051990 Avatar asked Sep 01 '25 22:09

user15051990


2 Answers

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
like image 78
jpp Avatar answered Sep 03 '25 23:09

jpp


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

category   Topic1 Topic2  Topic3
0           hello    hey      hi
1         valuess   name  python
like image 30
Tom Ron Avatar answered Sep 04 '25 01:09

Tom Ron