如何分析和连接多个csv文件中的行(python)

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

我有多个 csv 文件,其格式均为: 日期、时间、姓名 1、姓名 2、数字 1、数字 2、数字 3、数字 4、数字 5、数字 6、数字 7

我想要做的是通过连接不同文件中的行来获取最大数字 1-7,条件是日期、时间、名称 1、名称 2 相等。我找不到最好使用哪种结构的解决方案,以及我应该如何构建我的算法以尽可能优化。

唯一的想法就是对其进行暴力破解,并尝试将一个文件中的每一行与所有其他文件进行匹配,但其时间效率不高。

python data-analysis
1个回答
0
投票

您可以尝试以下操作吗:

import pandas as pd

# Define a list of file paths
file_paths = ["file1.csv", "file2.csv", "file3.csv", ...]  # Replace with actual file paths

# Initialize an empty DataFrame
df_combined = pd.DataFrame()

# Read each CSV file and append it to the combined DataFrame
for file_path in file_paths:
    df = pd.read_csv(file_path)
    df_combined = df_combined.append(df, ignore_index=True)

req_cols = ["number1","number2","number3","number4","number5","number6","number7"]
# Get the maximum value from each column
max_values = df_combined]req_cols[req_cols].max(axis=0)

# Print the maximum values
print(max_values)

© www.soinside.com 2019 - 2024. All rights reserved.