我的文件夹中有一系列 .csv 文件。每个都具有相同的格式,B 列标记为“users_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)
感谢任何帮助。如果需要,很乐意发布更多信息。
最佳答案
试试这个:
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())
关于python - 如何循环遍历文件夹中的文件以获取 Python 中列的最大值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61410243/