简体   繁体   English

在 Pandas 中删除一行 Dataframe 在 Dataframe 中添加一列

[英]Deleting a row in a Pandas Dataframe adds a column to the Dataframe

I want to delete a single row in a dataframe which gets generated based on a csv, and after the row gets deleted, the file should be saved again.我想删除基于 csv 生成的 dataframe 中的一行,删除该行后,应再次保存该文件。 But I have the problem, that everytime I delete a row, a column with the name "Unknown 0.x" gets added and I have no idea why.但我遇到了问题,每次我删除一行时,都会添加一个名为“Unknown 0.x”的列,我不知道为什么。 Here is my delete function:这是我删除的 function:

def delete_row(path, file_type, row):
    if file_type == 'csv':
        df = pd.read_csv(path)
        df.drop(row, inplace=True)
        df.to_csv(path)

这是我删除 2 行后的数据帧:

I tried different approaches for deleting a row, even with converting it into other file types first.我尝试了不同的删除行的方法,甚至先将其转换为其他文件类型。 And when I try this with.json or.txt the deletion works.当我用 .json 或 .txt 尝试此操作时,删除有效。 Only with.csv I have this problem只有.csv我有这个问题

This happens because every time you use df.to_csv() , the index is saved as a new column.发生这种情况是因为每次您使用df.to_csv()时,索引都会保存为一个新列。 You need to set index=False to solve the problem.您需要设置index=False来解决问题。 So your function would become:所以你的 function 会变成:

def delete_row(path, file_type, row):
    if file_type == 'csv':
        df = pd.read_csv(path)
        df.drop(row, inplace=True)
        df.to_csv(path, index=False) # --> You need to set index to false here

On a related note, if you have an index column in your csv file that you want to set as index, assign that column as index while reading the csv file.在相关说明中,如果您的 csv 文件中有一个要设置为索引的索引列,请在读取 csv 文件时将该列指定为索引。 For example:例如:

pd.read_csv(path, index_col=0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM