Pandas截取列的一部分
以股票代码为例:
型式为:6位数字+"."+交易所代码,如600028.SH
如只需保留前6位:
pattern = '(\w+)(?:.SZ|.SH)$'
df['股票代码'] = df['股票代码'].str.extract(pattern)
另外一种方式:
df['股票代码'] = df['股票代码'].str[0:6]
Pandas截取列的一部分的更多相关文章
- Pandas截取列部分字符,并据此修改另一列的数据
#截取'股票代码'第一个字符 df['首字符'] = df['股票代码'].str[0:1] ' # 根据'首字符'列的值,修改'市场'的值. 1表示上海 截取字符串的部分字符: date=today ...
- 【跟着stackoverflow学Pandas】 - Adding new column to existing DataFrame in Python pandas - Pandas 添加列
最近做一个系列博客,跟着stackoverflow学Pandas. 以 pandas作为关键词,在stackoverflow中进行搜索,随后安照 votes 数目进行排序: https://stack ...
- pandas 按照列A分组,将同一组的列B求和,生成新的Dataframe
对于pandas中的Dataframe,如果需要按照列A进行分组,将同一组的列B求和,可以通过下述操作完成: df = df.groupby(by=['column_A'])['column_B']. ...
- pandas 选择列或者添加列生成新的DataFrame
选择某些列 import pandas as pd # 从Excel中读取数据,生成DataFrame数据 # 导入Excel路径和sheet name df = pd.read_excel(exce ...
- pandas对列求和
了解更多,请关注公众号"轻松学编程" 一行代码实现对列求和 使用pandas把列表中的字典元素转成二维数组,然后使用pandas函数实现对每一列求和. 代码: import pan ...
- pandas 移动列的方法
import pandas as pd df = pd.DataFrame(np.random.randn(3,4),columns=['a','b','c','d']) k = df.pop(&qu ...
- 更改pandas dataframe 列的顺序
摘自 stackoverflow 这是我的df: Net Upper Lower Mid Zsore Answer option More than once a day 0% 0.22% -0.12 ...
- Java截取图片的一部分并保存为40*40的图片
@Test public void testImag() { try { String path = "E:/flower2.jpg"; int x = 11, y = 20, c ...
- pandas 多列排序
import pandas as pd df = pd.DataFrame({'AAA' : [1,2,1,3], 'BBB' : [1,1,2,2], 'CCC' : [2,1,3,1]}) sou ...
随机推荐
- [Golang] 编译程序时打上git提交信息标记
1.加入代码 //version.go package version import ( "flag" "fmt" "os" ) var ( ...
- python程序如何脱离ide而在操作系统上执行
IDE就像一个婴儿的摇篮,当程序开发好了之后,打包成一个在OS运行的软件,这是算法落地的重要一步.如果只能在IDE上运行,那这个软件有什么意义呢?接下来我就得想办法,把我的程序迁移到win操作系统上执 ...
- D - Nearest Common Ancestors
A rooted tree is a well-known data structure in computer science and engineering. An example is show ...
- Solve Docker for Windows error: docker detected, A firewall is blocking file Sharing between Windows and the containers
被这个“分享硬盘”问题烦了我好几个小时,终于在一个叫Marco Mansi外国人博客上找到解决方法了,真的很无奈 https://blog.olandese.nl/2017/05/03/solve-d ...
- java.sql.SQLException: Field 'id' doesn't have a default value(用eclipse操作数据库时报了这种奇怪的错误)的原因与解决方法
1.错误原因 由于id在数据库表中是作为主键,但是在插入的过程中,没有给予数值,并且没有让其自增 2.解决办法 修改数据库表中的id,让其自增(在插入的过程中,不插入id数据时)
- python全栈开发 * 24 知识点汇总 * 180705
24 模块-------序列化一.什么是模块 模块:py文件就是一个模块.二.模块的分类:(1)内置模块 (登录模块,时间模块,sys模块,os模块)(2)扩展模块 (itchat 微信有关,爬虫,b ...
- SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)【申明:来源于网络】
SSM框架--详细整合教程(Spring+SpringMVC+MyBatis)[申明:来源于网络] 地址:http://blog.csdn.net/u014662268/article/details ...
- linux_vim_emmet插件的安装配置
首先要去如下网址下载一个安装包(英文基础好的同学可以去github上搜他的开源,写的更加详细) https://www.vim.org/scripts/script.php?script_id=298 ...
- [Day7]循环、数组方法、排序查找
1. ASCII(American Standard Code for Information Interchange) (1)数字0-9对应ASCII编码十进制为48-57, 字母a-z对应ASCI ...
- java框架之SpringBoot(10)-启动流程及自定义starter
启动流程 直接从 SpringBoot 程序入口的 run 方法看起: public static ConfigurableApplicationContext run(Object source, ...