【发布时间】:2021-11-21 01:58:35
【问题描述】:
在我的数据框中,我有一列字符串,例如:
**type**
game/design_game
game/art_game
design/product design
fashion/accessories
games/tabletop games
art/digital art
art/public art
我想把它从 / 分成两部分:
main_cat subcat
game design_game
game art_game
我正在应用拆分功能:
df.column.str.split('/',n=1, expand = True)
但我只得到 main_cat 列而不是 subcats
我也尝试过:
# new data frame with split value columns
new = df["column"].str.split("/", n = 1, expand = True)
# making separate first name column from new data frame
df["subcat"]= new[1]
# making separate last name column from new data frame
df["main_cat"]= new[0]
# df display
df.head(2)
但是为 new[1] 获取 keyerror
谁能帮帮我。谢谢!
【问题讨论】:
-
对我来说工作正常。
-
是的,应该是,但不适合我。有没有办法检查原因?
-
像任何用于字符串操作的pythonic技术一样?
-
一个想法 - 检查
/是否通过df["column"].head().tolist()?如果没有其他字符,请尝试将/复制到df["column"].str.split("copied char", n = 1, expand = True) -
你在阅读 DF 时尝试过拆分吗?使用参数 sep="/"?
标签: pandas string dataframe split