我有這個熊貓df
show_id type title director cast
s1 Movie Duck the Halls Dave Wasson Chris Diamantopoulos, Tony Anselmo,
Tress MacNeille, Bill Farmer,
我需要能夠以多行的方式分解“cast”欄位示例:
show_id type title director cast
s1 Movie Duck the Halls Dave Wasson Chris Diamantopoulos
s1 Movie Duck the Halls Dave Wasson Tony Anselmo
s1 Movie Duck the Halls Dave Wasson Tress MacNeille
s1 Movie Duck the Halls Dave Wasson Bill Farmer
我知道我應該用熊貓來做,但這很復雜,你能幫我嗎?
uj5u.com熱心網友回復:
您可以使用吐和爆炸:
df['cast'] = df['cast'].str.split(',')
df.explode('cast')
uj5u.com熱心網友回復:
您可以使用:
out = (df.drop(columns='cast')
.join(df['cast'].str.split(',')
.explode()
)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/483167.html
