如何删除所有值低于一定阈值的列

发布于 2025-01-23 20:38:03 字数 349 浏览 0 评论 0原文

我正在尝试删除数据框中的任何列,这些列没有一个高于.9的值。我知道这可能不是最有效的方法,但我找不到问题。我知道这不是正确的,因为它只会删除一列,我知道它应该接近20。然后删除该列。提前致谢。

for i in range(len(df3.columns)):
    count=0
    for j in df3.iloc[:,i].tolist():
        if j<.9:
            count+=1
    
    if len(df3.iloc[:,i].tolist())==count:
        df4=df3.drop(df3.columns[i], axis=1)
df4

I am trying to remove any columns in my dataframe that do not have one value above .9. I know this probably isn't the most efficient way to do it but I can't find the problem with it. I know it isn't correct because it only removes one column and I know it should be closer to 20. So I do a count to see how many values are below .9 and then if the count equals the length of the list of column values then drop that column. Thanks in advance.

for i in range(len(df3.columns)):
    count=0
    for j in df3.iloc[:,i].tolist():
        if j<.9:
            count+=1
    
    if len(df3.iloc[:,i].tolist())==count:
        df4=df3.drop(df3.columns[i], axis=1)
df4

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花期渐远 2025-01-30 20:38:03

您可以在数据框中的每列循环,并根据所定义的阈值检查每个列中的最大值,在这种情况下为0.9,如果没有值超过0.9,则删除列。

输入:

    col1    col2    col3
0   0.2     0.8     1.0
1   0.3     0.5     0.5

代码:

# define dataframe
df = pd.DataFrame({'col1':[0.2, 0.3], 'col2':[0.8, 0.5], 'col3':[1, 0.5]})
# define threshold
threshold = 0.9

# loop through each column in dataframe
for col in df:
    # get the maximum value in column
    # check if it is less than or equal to the defined threshold
    if df[col].max() <= threshold:
        # if true, drop the column
        df = df.drop([col], axis=1)

此输出:

    col3
0   1.0
1   0.5

You can loop through each column in the dataframe and check the maximum value in each column against your defined threshold, 0.9 in this case, if there are no values more than 0.9, drop the column.

The input:

    col1    col2    col3
0   0.2     0.8     1.0
1   0.3     0.5     0.5

Code:

# define dataframe
df = pd.DataFrame({'col1':[0.2, 0.3], 'col2':[0.8, 0.5], 'col3':[1, 0.5]})
# define threshold
threshold = 0.9

# loop through each column in dataframe
for col in df:
    # get the maximum value in column
    # check if it is less than or equal to the defined threshold
    if df[col].max() <= threshold:
        # if true, drop the column
        df = df.drop([col], axis=1)

This outputs:

    col3
0   1.0
1   0.5
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文