在python中导出excel或csv文件时删除第一列

时间:2019-05-18 11:53:22

标签: python pandas spreadsheet-excel-writer

比方说,我在excel中导出了文件

声明导出

ln[] = writer = pd.ExcelWriter("df2018.xlsx")
writer = pd.ExcelWriter("df2018.xlsx", index = false)
df2018.to_excel(writer)
writer.save()

out[] = 
row0  (blank)  kanwil     jan 2018  feb 2018   janfeb 2018 
row1    0      sunarto    45878     545458     5454878
row2    1      hartanto   123456    545454     4444549
row3    2      pemabuk    9547      787896     4579798

(如果数据框为df2018)

我的期望

row0  kanwil     jan 2018  feb 2018   janfeb 2018 
row1  sunarto    45878     545458     5454878
row2  hartanto   123456    545454     4444549
row3  pemabuk    9547      787896     4579798

我要删除第一列

我尝试过

pd.ExcelWriter(“ df2018.xlsx”,索引=假)

但这不起作用

导出csv或excel时要删除第一列该怎么办

事实上,我从下面的数据框(df2018)中获取信息:

<class 'pandas.core.frame.DataFrame'>
Int64Index: 154 entries, 2 to 155
Data columns (total 5 columns):
Kanwil                 149 non-null object
Cabang                 137 non-null object
Income Januari 2018    151 non-null object
Income Febuari 2018    151 non-null object
Income Jan-Feb 2018    151 non-null object
dtypes: object(5)
memory usage: 4.2+ KB
None

2 个答案:

答案 0 :(得分:0)

我相信您需要在DataFrame.to_excel中指定参数index=False

pd.ExcelWriter("df2018.xlsx")
df.to_excel(writer, index = False)

答案 1 :(得分:0)