为了账号安全,请及时绑定邮箱和手机立即绑定

如何从用户输入中提取数据帧行

如何从用户输入中提取数据帧行

森栏 2023-07-05 11:08:26
data = {'Sample':['S1', 'S1', 'S1' ,'S1' ,'S2' ,'S2' ,'S3' ,'S3', 'S4', 'Negative', 'Positive', 'Negative',                 'S1', 'S1', 'S1' ,'S2' ,'S2' ,'S2' ,'S3' ,'S4', 'S4', 'Positive', 'Positive', 'Negative'],        'Location':['A1', 'A2', 'A3' ,'A4' ,'A5' ,'A6' ,'A7' ,'A8', 'A9', 'A10', 'A11', 'A12',                   'B1', 'B2', 'B3' ,'B4' ,'B5' ,'B6' ,'B7' ,'B8', 'B9', 'B10', 'B11', 'B12'],    'Repeat Number':['1', '2', '3' ,'4' ,'1' ,'2' ,'1' ,'2', '1', '1', '1', '2',                  '1', '2', '3' ,'1' ,'2' ,'3' ,'1' ,'1', '2', '1', '2', '1',],   'Identifier' :['asd01', 'asd02', 'asd03', 'asd04', 'asd05', 'asd06', 'asd07', 'asd08', 'asd09'                 ,'asd10' ,'asd11' ,'asd12' ,'asd13' ,'asd14' ,'asd15', 'asd16', 'asd17', 'asd18',                 'asd19', 'asd20', 'asd21', 'asd22', 'asd23', 'asd24']}df1 = pd.DataFrame(data) 在上图中,位置组 A 中有 4 个 S1,它们是重复的,因为它们位于同一位置组 A 中。对于位置 B,有 3 个 S1 并且它们是重复的,因为它们位于同一位置组 B 中。所以它们是给定重复编号(1,2,3,...)。对于上面的示例代码,我想为自己提取行,并在为“样本”、“位置”提供用户输入时重复该行。例如,如果我为“样本”输入 Negative,为“Location”输入 A,则理想结果将如下所示:data = {'Sample':[ 'Negative', 'Negative'],        'Location':[ 'A10',  'A12'],    'Repeat Number':[ '1', '2'],   'Identifier' : ['asd10' ,'asd12']}另外,我想知道如何在行选择后仅提取标识符。我尝试使用 df.loc[] 但我不知道如何进行用户输入,因为输入包含字符串
查看完整描述

4 回答

?
三国纷争

TA贡献1804条经验 获得超7个赞

使用下面的代码,您将能够从数据框中提取数据:


sample = input('Enter Sample: ')

location = input('Enter Location: ')

df.loc[(df['Sample'] == sample) & (df['Location'].str.contains(location))]

这是上面代码的输出:


Enter Sample: S2

Enter Location: B


    Sample  Location    Repeat Number   Identifier

15  S2  B4  1   asd16

16  S2  B5  2   asd17

17  S2  B6  3   asd18


查看完整回答
反对 回复 2023-07-05
?
交互式爱情

TA贡献1712条经验 获得超3个赞

只需链接您的条件和使用即可to_dict("list"):


print (df.loc[df["Sample"].eq("Negative")&df["Location"].str.contains("A")].to_dict("list"))


#{'Sample': ['Negative', 'Negative'], 'Location': ['A10', 'A12'], 'Repeat Number': ['1', '2'], 'Identifier': ['asd10', 'asd12']}


查看完整回答
反对 回复 2023-07-05
?
精慕HU

TA贡献1845条经验 获得超8个赞

以下内容会起作用。我相信在这种情况str.startswith下比str.contains:


import pandas as pd


data = {

    'Sample': [

        'S1', 'S1', 'S1', 'S1', 'S2', 'S2', 'S3', 'S3', 'S4', 'Negative', 'Positive', 'Negative',

        'S1', 'S1', 'S1', 'S2', 'S2', 'S2', 'S3', 'S4', 'S4', 'Positive', 'Positive', 'Negative'

    ],

    'Location': [

        'A1', 'A2', 'A3', 'A4', 'A5', 'A6', 'A7', 'A8', 'A9', 'A10', 'A11', 'A12',

        'B1', 'B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B8', 'B9', 'B10', 'B11', 'B12'

    ],

    'Repeat Number': [

        '1', '2', '3', '4', '1', '2', '1', '2', '1', '1', '1', '2',

        '1', '2', '3', '1', '2', '3', '1', '1', '2', '1', '2', '1'

    ],

    'Identifier': [

        'asd01', 'asd02', 'asd03', 'asd04', 'asd05', 'asd06', 'asd07', 'asd08', 'asd09',

        'asd10', 'asd11', 'asd12', 'asd13', 'asd14', 'asd15', 'asd16', 'asd17', 'asd18',

        'asd19', 'asd20', 'asd21', 'asd22', 'asd23', 'asd24'

    ]

}



location_start = 'A'

sample_result = 'Negative'


df1 = pd.DataFrame(data)


# filter on the two criteria

df2 = df1[df1['Location'].str.startswith(location_start, na=False) & (df1['Sample'] == sample_result)]


print(df2)

      Sample Location Repeat Number Identifier

9   Negative      A10             1      asd10

11  Negative      A12             2      asd12


查看完整回答
反对 回复 2023-07-05
?
海绵宝宝撒

TA贡献1809条经验 获得超8个赞

尝试这个:

df[(df.Sample=='Negative') & (df.Location.str.startswith('A'))]



查看完整回答
反对 回复 2023-07-05
  • 4 回答
  • 0 关注
  • 107 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信