如何在Python中循环浏览文件夹中的文件以获得列的最大值?

问题描述 投票:0回答:1

我在一个文件夹里有一系列.csv文件。 它们中的每一个都有相同的格式,B列标注为 "user_holding"。 我想循环浏览这个文件夹,并打印每个文件中B列的最大值。

import os
import pandas as pd

dirloc = r"C:\Users\username\Documents\folder"

for file in os.scandir(dirloc):
        if (file.path.endswith(".csv") or file.path.endswith(".pdf")) and file.is_file():
            a = pd.read_csv(file)

b = a['users_holding'].max()

print(b)

感谢任何帮助。 如果需要的话,我很乐意发布更多信息。

python python-3.x
1个回答
1
投票

你应该试试这个。

import pandas as pd
import os

for file in os.listdir(dirloc):
    if (file.endswith(".csv") and os.path.isfile(file)):

        file_full_path = os.path.join(dirloc, file)
        df=pd.read_csv(file_full_path)

        #FINDING MAX value
        p=df['ColumnName'].max()

        print(p)

它将检查每个 .csv 文件,并使用 pandas 并将打印出特定列的最大值,使用 .max() 功能。

希望对你有帮助...


2
投票

试试这个。

for file in os.listdir(dirloc):
    if file.endswith(".csv") and os.path.isfile(file):
        file_full_path = os.path.join(dirloc, file)
        df = pd.read_csv(file_full_path)
        print(df['users_holding'].max())
© www.soinside.com 2019 - 2024. All rights reserved.