Pandas将头(索引)添加到csv文件,即使index=False

y3bcpkx1  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(95)

我只是想做一些过滤的excel文件,并将其转换为csv,但由于某种原因,输出csv的第一行组成的列索引,我不能修复它。
这是我的代码

import pandas as pd
from FileHandler import select_save_file
def filter_by_action(input_file): 
    # Read the Excel file
    excel_file_path = input_file
    df = pd.read_excel(excel_file_path, header=None)


    # Filter rows where column C (index 2) equals '1'
    filtered_df = df[df[2].isin([1,3,5])]
    
    
    print(filtered_df)

    
    csv_file_path = select_save_file("csv")
    if not csv_file_path:
        return
    
    # Save the filtered data to a CSV file with UTF-8 encoding
    filtered_df.to_csv(csv_file_path, index=False, encoding='utf-8')

    print(f"Filtered data saved to: {csv_file_path}")

字符串
下面是从filtered_df中打印出来的内容(大致)

0   1   2   3   4   5   6         7              8         9   ...  76 77
0  IMM   1   1 NaN   0   0 NaN  IM - 118  Artikelnummer  IM - 118  ... NaN  0


我错过了什么,或者这不应该只是返回一个csv文件,只有零后的行(即IMM,1,1,NaN等)?任何帮助是非常感谢:)

bprjcwpo

bprjcwpo1#

如果您引用的是打印输出的第一行,则可能需要在to_csv函数中设置header=None
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.to_csv.html

相关问题