0

I'm trying to get columns in my data set from rows. Here is my present DataFrame:

DataFrame

What I want to achieve, is convert the following rows from column of index 0:

title:, category:, ..., href:

into columns without duplicates (there would be 7 columns) and match the corresponding rows from column of index 1 to them.

Column 1

I've tried to transpose this dataframe, but i didn't know how to reduce amount of columns.

Code:

df = pd.DataFrame(data=items)
df_2 = df.transpose()

Output:

Transposed DataFrame

What I've also tried:

pd.set_index(), pd.groupby(), pd.reset_index()

but it didn't work either.

Thank you so much for Your help. Have a nice day!

ozzu
  • 3
  • 1
  • 3

1 Answers1

1

Try this

df = pd.DataFrame([["title", "hp"],["category", "Laptop"], ["price", 30000], ["title", "lenovo"],["category", "Laptop"], ["price", 40000]],columns=["label", "value"])

print(df)

Output

      label      value
 0    title       hp
 1    category    Laptop
 2    price       30000
 3    title      lenovo
 4    category   Laptop
 5    price      40000

strong text

 label = df["label"].unique()
 a = df.groupby('label')
 df1 = pd.DataFrame()
 for i in label:
     df1[i] = list(a.get_group(i)['value'])

 print(df1)

Output

     title    category  price
 0   hp       Laptop    30000
 1   lenovo   Laptop    40000

Not sure what you are looking for.But this could help you