使用Pandas、Python从每一行文本中选择特定字符串

huangapple go评论68阅读模式
英文:

Selecting a specific string from a text in each row using Pandas, Python

问题

我一直在尝试提取以小写字母 'v' 开头的字符串。虽然在这里没有显示,但它们基本上是股票代码(例如,v109485)。我使用 csv_read 在 Python 中创建数据框。

我尝试过 df['STOCKS'].str.extract,甚至尝试拆分数据并尝试使用 str.startswith,但都没有成功。有人有什么想法吗?

英文:

I have been trying to extract strings that start with the lowercase letter 'v'. It's not shown here but they are basically stock numbers(e.g. v109485). I use csv_read to create the dataframe in Python.

I have tried df['STOCKS'].str.extract, even tried to split the data and try to use str.startswith but to no avail. Anyone got any ideas?

答案1

得分: 1

你可以使用这种方法:

df = pd.DataFrame({'STOCKS': ['v1234', 'x4567', 'v999', 'z111']})

df2 = df[df['STOCKS'].str.startswith('v')]

print(df2)

输出结果为:

  STOCKS
0  v1234
2   v999
英文:

Can you use this approach:

df = pd.DataFrame({'STOCKS': ['v1234', 'x4567', 'v999', 'z111']})

df2 = df[df['STOCKS'].str.startswith('v')]

print(df2)

gives:

  STOCKS
0  v1234
2   v999

huangapple
  • 本文由 发表于 2023年5月17日 23:27:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76273757.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定