Search code examples
pythonpandasdataframetranspose

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.


Solution

  • 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