英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论