• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

pandas数据框整个列的子字符串

用户头像
it1352
帮助1

问题说明

我有一个熊猫数据框"df".在此数据帧中,我有多个列,其中之一必须为子字符串. 可以说列名是"col". 我可以像下面那样运行"for"循环并对该列进行子串化:

I have a pandas dataframe "df". In this dataframe I have multiple columns, one of which I have to substring. Lets say the column name is "col". I can run a "for" loop like below and substring the column:

for i in range(0,len(df)):
  df.iloc[i].col = df.iloc[i].col[:9]

但是我想知道,是否有一个选项,我不必使用"for"循环,而直接使用属性进行操作.我有大量的数据,如果这样做,数据将花费很长时间.

But I wanted to know, if there is an option where I don't have to use a "for" loop, and do it directly using an attribute.I have huge amount of data, and if I do this, the data will take a very long time process.

正确答案

#1

在方括号内使用str访问器:

Use the str accessor with square brackets:

df['col'] = df['col'].str[:9]

str.slice :

df['col'] = df['col'].str.slice(0, 9)

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcfiggi
系列文章
更多 icon
同类精品
更多 icon
继续加载