python - 如何从新闻摘要中提取股票代码 NUMBER?
问题描述
我有一个 Pandas 表,需要从存储在列中的文本中提取股票代码“00981”、“00823”。代码采用 (00000) 格式。该代码将位于文本摘要中的不同位置。请指教。
News
1 example(00981)example example example。
2 example example example (00823)text text text
所需的输出:
Code column
981
823
s = TABLE['News'].str.find('(')
e = s + 5
c = TABLE['News'].str[s:e]
TABLE["Code"] = c
解决方案
这对我有用:
print(df)
News
0 1 example(00981)example example example。
1 2 example example example (00823)text text...
-
df['stock_num'] = df['News'].str.extract('(\d{5})').astype(int)
print(df)
News stock_num
0 1 example(00981)example example example。 981
1 2 example example example (00823)text text... 823
要将字符串更改为数字,您可以利用该.astype()
方法或pd.to_numeric(df['stock_number'])
推荐阅读
- variables - Windows 命令行脚本嵌入了空白字符
- c++ - 为什么 gl_FragCoord.z 与 ((pos.z / pos.w) + 1.0) * 0.5 不同?
- email - Yii2-批量发送邮件
- python - Pandas 中的列名 (Python)
- python - FP 增长(频繁模式挖掘)使用 Pyspark 给出错误
- php - 尝试通过 php 和 android 将图像上传到服务器时 size = 0
- java - 无法从 ListView 通过 Intent 正确传递数据
- php - PHP 将查询字符串显示为搜索词
- javascript - 我们怎样才能使情节动画流畅
- java - 在 JAR 资源中上一级