简体   繁体   English

如何在Pandas DataFrame的特定列中达到最小值后删除行?

[英]How to drop rows after minimum value reached in specific column in Pandas DataFrame?

If I have a pandas data frame like this: 如果我有这样的熊猫数据框:

      Col A  Col B
 0      9      2
 1      7      1
 2      6      9
 3      3      3
 4      1      4
 5      6      3
 6      7      2
 7      9      1

How do I remove all rows after the minimum value is reached in Column A (which is 1) such that I get a pandas data frame like this: 在达到列A的最小值(为1)之后,如何删除所有行,这样我得到一个熊猫数据框,如下所示:

      Col A  Col B
 0      9      2
 1      7      1
 2      6      9
 3      3      3
 4      1      4
df[df.index<=df['Col A'].idxmin()]

Try the below code, Hope it will help. 尝试下面的代码,希望对您有所帮助。

df = pd.DataFrame({'Col A': [9,7,6,3,1,6,7,9],'Col B':[2,1,9,3,4,3,2,1]})
df_list = df['Col A'].tolist()
index = df_list.index(min(df_list))
df.iloc[:index+1]

Ouput will be : Ouput将是:

  Col A     Col B
0   9       2
1   7       1
2   6       9
3   3       3
4   1       4

使用idxmin()

df = df.loc[:df['Col A'].idxmin()]

暂无
暂无

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

相关问题 如何删除带有条件的 Pandas DataFrame 行以保留特定列值 - How to drop Pandas DataFrame rows with condition to keep specific column value 如果特定列中的值不是 Pandas 数据框中的整数,则删除行 - Drop rows if value in a specific column is not an integer in pandas dataframe 如何删除Pandas DataFrame某列值为NaN的行 - How to drop rows of Pandas DataFrame whose value in a certain column is NaN Pandas - 如何根据唯一列值删除行,其中另一个列值是最小值并处理空值? - Pandas - How to drop rows based on a unique column value where another column value is a minimum and handling nulls? 如何在熊猫数据框中的列中删除带有“nan”的行? - how to drop rows with 'nan' in a column in a pandas dataframe? 过滤分组的熊猫数据框,保留列中具有最小值的所有行 - Filter grouped pandas dataframe, keep all rows with minimum value in column 根据等于 pandas dataframe 中的特定值的列定位最小日期? - Locating minimum date based on column equal to specific value in pandas dataframe? Pandas GroupBy 和 select 行在特定列中具有最小值 - Pandas GroupBy and select rows with the minimum value in a specific column 如何逐列识别整个 pandas dataframe 的最小平方值? - How to identify minimum squared value of an entire pandas dataframe column by column? 在pandas数据框的任何列中删除带有“问号”值的行 - Drop rows with a 'question mark' value in any column in a pandas dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM