Home > Enterprise >  Importing CSV into pandas with one column that contains all column names and another containing all
Importing CSV into pandas with one column that contains all column names and another containing all

Time:07-22

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?

CodePudding user response:

Try:

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

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

CodePudding user response:

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")
  • Related