简体   繁体   English

大熊猫:通过列的值提取某些行作为数据框

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

I have a list of column value and from a existing dataframe I want to extract the row by this column value by using a loop since the list has lots of values. 我有一个列值的列表,并且我想从现有数据框中通过循环使用此列值提取行,因为该列表包含很多值。

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

both does not get the result I want. 两者都没有得到我想要的结果。

Use isin for getting the rows which have given list values as: 使用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

So, the column d has 7 and 19 in 1st and 4th rows which we selected using isin . 因此, column d在我们使用isin选择的第一行和第四行中具有719

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Pandas 数据框过滤特定列值后的 n 个后续行 - Pandas dataframe filter n subsequent rows after certain column value 如何删除Pandas DataFrame某列值为NaN的行 - How to drop rows of Pandas DataFrame whose value in a certain column is NaN 返回 pandas dataframe 中列中的元组包含特定值的行 - Return rows in pandas dataframe where tuple in column contains a certain value 选择列中具有特定值的熊猫数据框的前几行 - Select first few rows of pandas dataframe with a certain value in the column 将 DataFrame 中某些列和行的值替换为同一 dataframe 和 Pandas 中的另一列的值 - Replace values of certain column and rows in a DataFrame with the value of another column in the same dataframe with Pandas Pandas数据帧,将列的连续行提取到列表中 - Pandas dataframe, extract consecutive rows of a column to a list 如何仅合并 pandas dataframe 中某列的行中没有值的行 - How to merge only on rows where there is no value in the rows of a certain column in pandas dataframe if else 条件在 pandas 数据框中并提取列值 - if else conditions in pandas dataframe and extract column value 在 Pandas 数据框中提取列列表包含某些值的行 - Extract rows where the lists of columns contain certain values in a pandas dataframe pandas 获取具有特定值的行的列平均值? - pandas get column average for rows with a certain value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM