基于pandas中expand的作用详解
作者:东方朔盗仙桃 时间:2022-02-04 06:53:53
expand表示是否把series类型转化为DataFrame类型
下面代码中的n表示去掉下划线"_"的数量
代码如下:
import numpy as np
import pandas as pd
s2 = pd.Series(['a_b_c_f_j', 'c_d_e_f_h', np.nan, 'f_g_h_x_g'])
print("-----------------------------------")
print(s2.str.split('_'))
print("-----------------------------------")
print(s2.str.split('_').str.get(1))
print("-----------------------------------")
print(s2.str.split('_').str[1])
print("---------------expand=True--------------------")
expand1=s2.str.split('_', expand=True)
print(expand1)
print(type(expand1))
print("---------------expand=False--------------------")
expand2=s2.str.split('_', expand=False)
print(expand2)
print(type(expand2))
print("##########################################################")
print("---------------expand=True,n=1--------------------")
expand1=s2.str.rsplit('_', expand=True,n=1)
print(expand1)
print("---------------expand=False,n=1--------------------")
expand2=s2.str.rsplit('_', expand=False,n=1)
print(expand2)
运行结果如下:
-----------------------------------
0 [a, b, c, f, j]
1 [c, d, e, f, h]
2 NaN
3 [f, g, h, x, g]
dtype: object
-----------------------------------
0 b
1 d
2 NaN
3 g
dtype: object
-----------------------------------
0 b
1 d
2 NaN
3 g
dtype: object
---------------expand=True--------------------
0 1 2 3 4
0 a b c f j
1 c d e f h
2 NaN NaN NaN NaN NaN
3 f g h x g
<class 'pandas.core.frame.DataFrame'>
---------------expand=False--------------------
0 [a, b, c, f, j]
1 [c, d, e, f, h]
2 NaN
3 [f, g, h, x, g]
dtype: object
<class 'pandas.core.series.Series'>
##########################################################
---------------expand=True,n=1--------------------
0 1
0 a_b_c_f j
1 c_d_e_f h
2 NaN NaN
3 f_g_h_x g
---------------expand=False,n=1--------------------
0 [a_b_c_f, j]
1 [c_d_e_f, h]
2 NaN
3 [f_g_h_x, g]
dtype: object
[Finished in 0.4s]
来源:https://blog.csdn.net/appleyuchi/article/details/102531498
标签:pandas,expand
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
解析arp病毒背后利用的Javascript技术
2007-08-08 09:55:00
ASP中Session技巧 默认过期时间为20分钟
2012-12-04 20:28:26
如何用python绘制雷达图
2023-04-19 12:44:09
![](https://img.aspxhome.com/file/2023/4/78544_0s.jpg)
Python反爬实战掌握酷狗音乐排行榜加密规则
2021-05-29 08:05:57
![](https://img.aspxhome.com/file/2023/5/70245_0s.gif)
Oracle10g 安装方法
2009-06-19 17:50:00
vscode 远程调试python的方法
2021-09-07 23:00:59
![](https://img.aspxhome.com/file/2023/7/83717_0s.png)
python自动截取需要区域,进行图像识别的方法
2021-05-14 11:04:42
sqlserver 2000中创建用户的图文方法
2012-03-26 18:26:39
使用面向对象的技术创建高级JavaScript Web 应用程序
2008-11-03 13:00:00
![](https://img.aspxhome.com/file/UploadPic/200811/3/code-21s.gif)
Django基础CBV装饰器和中间件的应用示例
2023-09-30 06:17:07
![](https://img.aspxhome.com/file/2023/2/78832_0s.jpg)
采用XMLHTTP编写一个天气预报的程序
2007-10-15 12:35:00
Django serializer优化类视图的实现示例
2021-11-23 23:16:46
![](https://img.aspxhome.com/file/2023/2/86402_0s.png)
Python超简单分析评论提取关键词制作精美词云流程
2021-07-27 22:00:41
![](https://img.aspxhome.com/file/2023/3/67833_0s.png)
ASP编写完整的一个IP所在地搜索类
2007-10-18 10:43:00
Windows下Apache+MySQL+PHP运行环境的安装图文方法
2023-10-14 02:05:31
![](https://img.aspxhome.com/file/2023/1/65701_0s.png)
详解Python中__str__和__repr__方法的区别
2023-02-28 17:29:17
Python实现字符串与数组相互转换功能示例
2021-08-13 15:50:12
![](https://img.aspxhome.com/file/2023/3/76883_0s.jpg)
用javascript实现页面无刷新更新数据
2011-08-14 07:54:08
Python中super().__init__()测试以及理解
2023-08-25 02:22:04
Oracle使用PL/SQL操作COM对象
2010-07-21 12:56:00