-2

I have a .csv label and have four different categories.

and now my .csv file looks like this:

id type

1 1

2 2

3 3

4 4

5 2

...

I want to convert it to like:

id type1 type2 type3 type4

1 1 0 0 0

2 0 1 0 0

3 0 0 1 0

4 0 0 0 1

5 0 1 0 0

how can I done these via python? I use pd.read_csv()

  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Mar 30 '22 at 15:39

1 Answers1

0

If your csv-file (named here file.csv) looks like

id,type
1,1
2,2
3,3
4,4
5,2

then you could use .str.get_dummies() to do

df = (
    pd.read_csv("file.csv", index_col=0)
    .type.astype(str).str.get_dummies().rename(lambda c: f"type{c}", axis=1)
)

to get the following dataframe

    type1  type2  type3  type4
id                            
1       1      0      0      0
2       0      1      0      0
3       0      0      1      0
4       0      0      0      1
5       0      1      0      0

If you want to write that back to a new csv-file, then

df.to_csv("file_new.csv", index=True)

produces the following file file_new.csv:

id,type1,type2,type3,type4
1,1,0,0,0
2,0,1,0,0
3,0,0,1,0
4,0,0,0,1
5,0,1,0,0
Timus
  • 10,974
  • 5
  • 14
  • 28
  • wow ! Thanks for that answer! although i have already use simple if-else to solve that . Anyway, Thanks a lot! – Agnesia Apr 04 '22 at 19:52