Zen Zac
Zen Zac

Reputation: 136

Importing CSV into pandas with one column that contains all column names and another containing all the values

I want to import a CSV into pandas. I currently use a for loop and turn the CSV into a dictionary before turning it into a data frame.

The data looks like this:

| row id | attr name | attr value |
| ______ | _________ | __________ |
| 5      | beans.    | 1.         | 
| 5.     | fruit.    | 2          |
| 5.     | claw.     | 2          |
| 5.     | house.    | 1          |
| 5.     | nuts.     | 0          |
| 6.     | beans.    | 0          |
| 6.     | fruit.    | 1          |
| 6.     | claw.     | 2          |
| 6.     | house.    | 3          |
| 6.     | nuts.     | 0          |

And I want it to look like this:

| row id | beans | fruit | claw | house | nuts |
| ______ | _____ | _____ | ____ | _____ | ____ |
| 5.     | 1     | 2     | 2    | 1     | 0    |
| 6.     | 0     | 1     | 2    | 3     | 0    |

Is this at all possible to do automatically? Or should I just stick to my for loop that reads it in row-by-row?

Upvotes: 0

Views: 63

Answers (2)

abmuvak
abmuvak

Reputation: 26

What you want to do is to reshape the frame. You can achieve this through a pivot

df.pivot(index="id",  columns="name", values="value")

Upvotes: 1

dolak
dolak

Reputation: 21

Try:

df.pivot(index='row id', columns=['attr name'], values=['attr value'])

https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.pivot.html

Upvotes: 2

Related Questions