gpt4 book ai didi

python Pandas : Split slash separated strings in two or more columns into multiple rows

转载 作者:行者123 更新时间:2023-11-28 21:09:12 24 4
gpt4 key购买 nike

我有一个看起来像这样的 Pandas 数据框:

SUBJECT                 STUDENT         CITY        STATE

Math/Chemistry/Biology Sam/Peter/Mary Los Angeles CA
Geology/Physics John Boston MA

它应该是这样的:

SUBJECT      STUDENT    CITY           STATE

Math Sam Los Angeles CA
Chemistry Peter Los Angeles CA
Biology Mary Los Angeles CA
Geology John Boston MA
Physics John Boston MA

在问这个问题之前,我引用了这个页面中提到的解决方案: pandas: How do I split text in a column into multiple rows?

由于两列中有斜线分隔的字符串,我无法使用上述链接中的解决方案。

最佳答案

另一种解决方案 concatjoin :

s1 = df.SUBJECT.str.split('/', expand=True).stack()
s2 = df.STUDENT.str.split('/', expand=True).stack()
print (s1)
0 0 Math
1 Chemistry
2 Biology
1 0 Geology
1 Physics

print (s2)
0 0 Sam
1 Peter
2 Mary
1 0 John
dtype: object
df1 = pd.concat([s1,s2], axis=1, keys=('SUBJECT','STUDENT'))
.ffill()
.reset_index(level=1, drop=True)
print (df1)
SUBJECT STUDENT
0 Math Sam
0 Chemistry Peter
0 Biology Mary
1 Geology John
1 Physics John

df = df.drop(['SUBJECT','STUDENT'], axis=1)
.join(df1)
.reset_index(drop=True)[['SUBJECT', 'STUDENT', 'CITY','STATE']]
print (df)
SUBJECT STUDENT CITY STATE
0 Math Sam Los Angeles CA
1 Chemistry Peter Los Angeles CA
2 Biology Mary Los Angeles CA
3 Geology John Boston MA
4 Physics John Boston MA

关于 python Pandas : Split slash separated strings in two or more columns into multiple rows,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38709423/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com