Df

fruit                want_split
apple    colour:red, like:yes, favourite:no
banana   colour:yellow, like:yes, favourite:no
pear     colour:green, like:yes, favourite:yes

Apakah ada cara untuk membagi dengan koma dan kemudian secara otomatis menghasilkan nama kolom, daripada secara manual? Keluaran yang diharapkan:

fruit                want_split                  colour      like    favourite
apple    colour:red, like:yes, favourite:no     red        yes       no
banana   colour:yellow, like:yes, favourite:no  yellow     yes       no
pear     colour:green, like:yes, favourite:yes  green      yes       yes
2
asd 3 Januari 2021, 19:38

3 jawaban

Jawaban Terbaik

Anda dapat menggunakan pivot / unstack dan join setelah str.split dan explode:

u = df['want_split'].str.split(",").explode().str.split(":",expand=True)
out = df.join(u.set_index(0,append=True)[1].unstack())

print(out)

     type                             want_split  favourite  like  colour
0   apple     colour:red, like:yes, favourite:no         no   yes     red
1  banana  colour:yellow, like:yes, favourite:no         no   yes  yellow
2    pear  colour:green, like:yes, favourite:yes        yes   yes   green
3
anky 3 Januari 2021, 16:55

Anda dapat menggunakan .str.extractall untuk mengekstrak grup tangkapan dalam pola regex sebagai kolom dalam kerangka data.

c = ['colour','like','favourite']
df[c] = df['want_split'].str.extractall(r':([^,]+)')[0].unstack()

Atau Anda dapat mencoba .str.findall:

df[c] = df['want_split'].str.findall(r':([^,]+)').tolist()

    fruit                             want_split  colour like favourite
0   apple     colour:red, like:yes, favourite:no     red  yes        no
1  banana  colour:yellow, like:yes, favourite:no  yellow  yes        no
2    pear  colour:green, like:yes, favourite:yes   green  yes       yes
1
Shubham Sharma 3 Januari 2021, 16:54

Mari kita coba split dengan dict , lalu buat kolom tambahan

out = df.join(pd.DataFrame(df.want_split.map(lambda s : dict(item.split(":") for item in s.split(", "))).tolist(),index=df.index))
    fruit                           want_split  colour like favourite
0   apple     colour:red,like:yes,favourite:no     red  yes        no
1  banana  colour:yellow,like:yes,favourite:no  yellow  yes        no
2    pear  colour:green,like:yes,favourite:yes   green  yes       yes
1
BENY 3 Januari 2021, 17:11