我正在编写一个Python脚本,它可以解析目录中的所有文件夹,并将多个文件夹中的所有数据存储到一个Panda数据框中,然后将数据框保存到CSV文件中,但当我尝试运行此代码时,我会收到一个错误
错误:重新索引仅对唯一值的Index对象有效
import os
import pandas as pd
def read_excel_files_in_folder(folder_path):
data = []
for root, dirs, files in os.walk(folder_path):
for file in files:
if file.endswith(".xlsx"):
file_path = os.path.join(root, file)
df = pd.read_excel(file_path)
# Remove duplicate rows
df = df.drop_duplicates()
# Remove leading and trailing whitespaces from string columns
df = df.apply(lambda x: x.str.strip() if x.dtype == "O" else x)
data.append({'File': file_path, 'Data': df})
return data
def main(input_folder, output_csv):
# desired column
columns = [
# all the column in the excell file
]
main_df = pd.DataFrame(columns=columns)
# Read data from all Excel files in the given folder
data = read_excel_files_in_folder(input_folder)
# Populate the main DataFrame with the cleaned data
for entry in data:
entry_df = pd.DataFrame({'File': [entry['File']], 'Data': [entry['Data']]})
# Check for duplicates in index
if main_df.index.duplicated().any():
main_df.reset_index(drop=True, inplace=True)
main_df = pd.concat([main_df, entry_df], ignore_index=True)
# Save the main DataFrame to a CSV file
main_df.to_csv(os.path.join(output_csv, 'Datalab_output_data.csv'), index=False)
if __name__ == "__main__":
input_folder = r"C:\Users\Edopi\OneDrive\Desktop\2022_2023"
output_csv = r'C:\Users\Edopi\OneDrive\Desktop\2023-24b-fai1-adsai-EdoardoPierezza231412\Deliverables\Data_lab_preparetion'
main(input_folder, output_csv)
字符串
我正在编写一个Python脚本,它可以解析目录中的所有文件夹,并将多个文件夹中的所有数据存储到一个panda数据框中,然后将我的数据框保存到csv文件中
1条答案
按热度按时间xtupzzrd1#
问题可能发生在
concat
函数中,这是由于重复索引造成的。尝试添加
concat
函数:ignore_index=True