【发布时间】:2023-03-17 13:26:01
【问题描述】:
我正在处理一个数据集,其中列被命名为标题。值如前所述。
df = pd.DataFrame(data={"location":["düsseldorf, nordrhein-westfalen, germany",
"durbanville , cape town, cape town , south africa"]})
我想把这个专栏分成['city', 'state', 'country']。注意第二行有重复。
我已经尝试了以下方法,但这并不处理重复:
location = df.location.str.split(', ', n=2, expand=True)
location.columns = ['city', 'state', 'country']
【问题讨论】:
-
你想要的输出是什么?你试过什么?
-
location=df.location.str.split(', ', n=2, expand=True)location.columns=['city', 'state', 'country'] -
但我无法处理重复问题
标签: python string python-3.x pandas dataframe