如何删除行名为 None 的空行

How to drop a a empty row with the row name called None

我想创建一个如下所示的 pandas 数据框:

>>> open csv_file.csv

然后,由于我使用的是 MacBook,它会打开 Numbers。我希望它看起来像这样:

    ___________________________________________________________________________________
    |How was your day?|Did you spend money?| How much money did you spend?| Description|
   0|     good        |          yes       |                           |we ate pizza!|

但是,我得到了这个:

    ___________________________________________________________________________________
    |How was your day?|Did you spend money?| How much money did you spend?| Description|
   0|     good        |          yes       |                           |we ate pizza!|
None|                 |                    |                              |             |

这是它的代码:

from PySide6 import QtWidgets as qtw


cal = qtw.QCalendarWidget()
date = cal.selectedDate()
saving_columns_show = {"How was your day" : "good","Did you spend money":"no" , "How much money did you spend":"nope", "Description":"nothin special"}
item_price_show = {"item name":"nope", "item price":"nipe"}

name = .date.toString("yyyy")+"_"+.date.toString("M")+"_"+.date.toString("d")
df_columns_by_date = pd.DataFrame(saving_columns_show, index = [0])
print("shape",.df_columns_by_date.shape)
print("df",.df_columns_by_date)
df_item_price = pd.DataFrame(item_price_show, index = [0])
file_1 = open(str(name)+ ".csv",'w')
df_columns_by_date.drop(labels=[1], axis=0)
create_write=.df_columns_by_date.to_csv(file_1, sep=",")
print(type(create_write))
file_1.write(str(create_write))
file_1.close()

我想这就是您所需要的。即使它不是整个程序。如果我没有包含代码中使用的内容,请告诉我。

无论如何,我尝试查看其他 Whosebug 问题,但找不到任何问题。我尝试使用 replace,但许多其他人都找不到。我该如何解决这个问题?

试试这个:

(替换为你的数据框名称)还有你为什么不打开 pandas? df = pd.read_csv()

df.drop(['None'], inplace=True)

好的,我知道怎么丢了!结果是:create_write=.df_columns_by_date.to_csv(file_1, sep=",") 行实际上已经创建了一个 .csv 文件。所以,我所要做的就是删除这些行:

file_1 = open(str(name)+ ".csv",'w')
file_1.write(str(create_write))
file_1.close()