创建一个从特定字符串开始的循环 pandas

Create a loop startswith specific string pandas

我还是 python 初学者。我只想提取以特定前缀开头的记录,例如 'Wrong Data' for 'Specific Group' from df:

我正在尝试创建一个循环,请看下面:

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

但是这个循环并没有提取我想要的东西。我不确定出了什么问题。你能帮忙吗?

pandas 的妙处在于您不必循环执行这些操作。

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'“魔术索引”。那些比较运算符 return 布尔数组,条件为真时为真。将其传递给 df[...] 时,return 仅包含数组元素为 True 的行。

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

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