如何在 pandas 中将一列分为三列

编程入门 行业动态 更新时间:2024-10-25 08:25:37
本文介绍了如何在 pandas 中将一列分为三列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个如下所示的数据框

I have a data frame as shown below

ID Name Address 1 Kohli Country: India; State: Delhi; Sector: SE25 2 Sachin Country: India; State: Mumbai; Sector: SE39 3 Ponting Country: Australia; State: Tasmania 4 Ponting State: Tasmania; Sector: SE27

从上面我想在下面的数据框里准备

From the above I would like to prepare below data frame

ID Name Country State Sector 1 Kohli India Delhi SE25 2 Sachin India Mumbai SE39 3 Ponting Australia Tasmania None 4 Ponting None Tasmania SE27

我尝试了以下代码

df[['Country', 'State', 'Sector']] = pd.DataFrame(df['ADDRESS'].str.split(';',2).tolist(), columns = ['Country', 'State', 'Sector'])

但是从上面再次,我必须通过对列进行切片来清理数据.我想知道有没有比这更简单的方法.

But from the above again I have to clean the data by slicing the column. I would like to know is there any easy method than this.

推荐答案

将列表理解和dict理解用于字典列表,并传递给DataFrame构造函数:

Use list comprehension with dict comprehension for list of dictionaries and pass to DataFrame constructor:

L = [{k:v for y in x.split('; ') for k, v in dict([y.split(': ')]).items()} for x in df.pop('Address')] df = df.join(pd.DataFrame(L, index=df.index)) print (df) ID Name Country State Sector 0 1 Kohli India Delhi SE25 1 2 Sachin India Mumbai SE39 2 3 Ponting Australia Tasmania NaN

或将split与重塑stack一起使用:

df1 = (df.pop('Address') .str.split('; ', expand=True) .stack() .reset_index(level=1, drop=True) .str.split(': ', expand=True) .set_index(0, append=True)[1] .unstack() ) print (df1) 0 Country Sector State 0 India SE25 Delhi 1 India SE39 Mumbai 2 Australia NaN Tasmania df = df.join(df1) print (df) ID Name Country Sector State 0 1 Kohli India SE25 Delhi 1 2 Sachin India SE39 Mumbai 2 3 Ponting Australia NaN Tasmania

更多推荐

如何在 pandas 中将一列分为三列

本文发布于:2023-10-27 03:34:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1532195.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中将   为三   如何在   pandas

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!