亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何從用戶輸入中提取數據幀行

如何從用戶輸入中提取數據幀行

森欄 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 關注
  • 182 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號