hoangftran125
hoangftran125

Reputation: 21

Change label value to column dataframe

My dataframe looks like this:

 sample  intent
0   a   intent 1
1   b   intent 1
2   c   intent 1
3   x   intent 2
4   y   intent 2
5   z   intent 2

I want my dataframe looks like this:

  intent 1 intent 2
0    a       x        
1    b       y
2    c       z

Thank you very much.

Upvotes: 1

Views: 447

Answers (2)

hoangftran125
hoangftran125

Reputation: 21

I have found the answer:

df = df.groupby([df.index, 'intent'])['sample'].first().unstack()

Upvotes: 1

Hietsh Kumar
Hietsh Kumar

Reputation: 1329

Hi I have just taken your df through excel... hope below lines solves your query

import pandas as pd
import numpy

df = pd.read_excel('excel.xlsx',sheet_name='Sheet1')

df1 = df.reset_index().pivot(columns='Intent', values='Sample')
df2 = df1.apply(lambda x: pd.Series(x.dropna().values))

print(df2)

Upvotes: 0

Related Questions