簡體   English   中英

大熊貓:通過列的值提取某些行作為數據框

[英]pandas: extract certain rows as a dataframe by the value of a column

我有一個列值的列表,並且我想從現有數據框中通過循環使用此列值提取行,因為該列表包含很多值。

list = [2,4,5,6,7,8, ....., 2345]
df # df is an existing dataframe 
#'number' is the name of the column that has the value of list in it 

for i in list:
    df.loc[(df["number"] == i)])
df

for i in list:
   P = pd.DataFrame(df.loc[(df["number"] == i)])
P # extract only one column of a certain number

兩者都沒有得到我想要的結果。

使用isin獲取具有給定列表值的行:

df = pd.DataFrame(np.arange(0,20).reshape(5,4),columns=list('abcd'))
    a   b   c   d
0   0   1   2   3
1   4   5   6   7
2   8   9  10  11
3  12  13  14  15
4  16  17  18  19

l = [2,7,19]

df.loc[df['d'].isin(l)]

    a   b   c   d
1   4   5   6   7
4  16  17  18  19

因此, column d在我們使用isin選擇的第一行和第四行中具有719

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM