创建一个循环startswith特定的字符串pandas

发布于 2025-01-27 10:35:57 字数 525 浏览 3 评论 0原文

我仍然是Python的初学者。我只想从df:

“ nofollow noreferrer”>的“ https://i.sstatic.net/gzuid.png”中,仅从特定前缀开始。 “在此处输入图像说明”

我正在尝试创建一个循环,请参见

names_list = []
for name in df['short_desc']:
    if 'Specifc Group' in df['group']:
        if name.startswith("Wrong Data"):
            names_list.append(name)

下文t提取我想拥有的东西。我不确定出了什么问题。你能帮忙吗?

I'm still a python beginner. I would like to extract only records starts with specific prefix like 'Wrong Data' for 'Specific Group' from df:

enter image description here

I'm trying to create a loop, please see below:

names_list = []
for name in df['short_desc']:
    if 'Specifc Group' in df['group']:
        if name.startswith("Wrong Data"):
            names_list.append(name)

But this loop doesn't extract what I would like to have. I'm not sure what went wrong. Could you please help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

这个俗人 2025-02-03 10:35:57

关于熊猫的很酷的事情是,您不必在循环中做这些事情。

import pandas as pd
data = [
    ['Closed', 'j.snow', 'Wrong Data.  Contact your admin', 'Specific Group'],
    ['Closed', 'j.doe', 'General Issue', 'Master Group'],
    ['Closed', 'j.snow', 'Wrong Data.  Contact your admin', 'Specific Group'],
    ['Closed', 'm.smith', 'Wrong Data.  Contact your admin', 'Specific Group'],
    ['Closed', 'a.richards', 'Wrong Data.  Contact your admin', 'Specific Group'],
    ['Closed', 'a.blecha', 'General Issue', 'Master Group'],
    ['Closed', 'r.kipling', 'Wrong Data.  Contact your admin', 'First Group']
]

df = pd.DataFrame(data, columns=['status', 'created', 'short_desc', 'group'])
print(df)
# Pick only those rows where short_desc starts with "Wrong".
df1 = df[df['short_desc'].str.startswith('Wrong')]
# Pick only those rows where group is "Specific Group".
df1 = df1[df1['group']=='Specific Group'] 
# Print the "short_desc" column.
print(df1['short_desc'])

或者,一行:

df1 = df[
        (df['short_desc'].str.startswith('Wrong')) &
        (df['group']=='Specific Group')
    ] 

这是Pandas的“魔术索引”。这些比较操作员返回一系列布尔值,在情况为真的地方。将其传递到df [...]时,仅返回数组元素为true的行。

The cool thing about pandas is that you don't have to do these things in a loop.

import pandas as pd
data = [
    ['Closed', 'j.snow', 'Wrong Data.  Contact your admin', 'Specific Group'],
    ['Closed', 'j.doe', 'General Issue', 'Master Group'],
    ['Closed', 'j.snow', 'Wrong Data.  Contact your admin', 'Specific Group'],
    ['Closed', 'm.smith', 'Wrong Data.  Contact your admin', 'Specific Group'],
    ['Closed', 'a.richards', 'Wrong Data.  Contact your admin', 'Specific Group'],
    ['Closed', 'a.blecha', 'General Issue', 'Master Group'],
    ['Closed', 'r.kipling', 'Wrong Data.  Contact your admin', 'First Group']
]

df = pd.DataFrame(data, columns=['status', 'created', 'short_desc', 'group'])
print(df)
# Pick only those rows where short_desc starts with "Wrong".
df1 = df[df['short_desc'].str.startswith('Wrong')]
# Pick only those rows where group is "Specific Group".
df1 = df1[df1['group']=='Specific Group'] 
# Print the "short_desc" column.
print(df1['short_desc'])

Or, in a single line:

df1 = df[
        (df['short_desc'].str.startswith('Wrong')) &
        (df['group']=='Specific Group')
    ] 

This is pandas' "magic indexing". Those comparison operators return an array of booleans, True where the condition is true. When passing that to df[...], that returns only the rows where the array element is True.

我很OK 2025-02-03 10:35:57

您需要使用.str.startswith查找列以特定值开头的行:

subset = df[df['short_desc'].str.startswith('Wrong Data') & df['group'].eq('Specific Group')]

You need to use .str.startswith to find rows where a column starts with a particular value:

subset = df[df['short_desc'].str.startswith('Wrong Data') & df['group'].eq('Specific Group')]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文