简体   繁体   中英

Creating columns based on individual values in rows pandas

Looking to simplify a huge dataset via pandas which has four columns,

        Time        A   B     C
    27/5/2020 1:30 -90 -12  0 2 3 5 6
    27/5/2020 1:35 -90 -11  0 2 3 4 6 7 8
    27/5/2020 1:40 -80 -12  2 4 5 6 9 12 15

I want to create a new dataframe which can give me all the existing columns and create new columns with value 1 based on the current numbers positions in columns C for each row.

Like;

Time             A   B      C             Col1 Col2 Col3 Col4 Col5 Col6 Col7
27/5/2020 1:30 -90 -12  0 2 3 5 6          1   0    1    1    0     1   1

and so on.. any help will be much appreciated. I am working through some for loops at the moment

Let us try get_dummies

df=df.join(df.C.str.get_dummies(' ').add_prefix('col'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM