![](/img/trans.png)
[英]python pandas new row attached to last one in csv when using to_csv in append mode
[英]How not to show/dipslay an index in csv when adding a new row using python pandas
我有一個CSV文件,我曾經從列中獲取其值,但我的問題是我得到的值來自兩個標題,即Rounded-Download-Speed和Rounded-Upload-Speed所以我所做的是分開它們並連接它。
然后我添加了兩行將它們分開,但問題是行顯示的索引,但我想要的是一個空白單元格。 這是我的代碼:
import pandas as pd
from decimal import Decimal, ROUND_HALF_UP
L=['0000','0100','0200','0300','0400','0500','0600'
,'0700','0800','0900','1000','1100','1200','1300'
,'1400','1500','1600','1700','1800','1900','2000'
,'2100','2200','2300']
df1 = pd.read_csv('Sample.csv')
df1.Date = pd.to_datetime(df1.Date, dayfirst=True)
df1 = df1.pivot_table(values='Rounded-Download-Speed',index='Date',columns='HourBin',aggfunc='max',fill_value="ND")
df1.columns = df1.columns.astype(str).str.zfill(4)
df1.index = df1.index.map(lambda t: t.strftime('%Y-%m-%d'))
df1 = df1.reindex_axis(L, axis=1)
spaceRow1 = "-"
df1.loc[len(df1)] = spaceRow1
spaceRow2 = L
df1.loc[len(df1)] = spaceRow2
df2 = pd.read_csv('Sample.csv')
df2.Date = pd.to_datetime(df2.Date, dayfirst=True)
df2 = df2.pivot_table(values='Rounded-Upload-Speed',index='Date',columns='HourBin',aggfunc='max',fill_value="ND")
df2.columns = df2.columns.astype(str).str.zfill(4)
df2.index = df2.index.map(lambda t: t.strftime('%Y-%m-%d'))
df2 = df2.reindex_axis(L, axis=1)
df3 = pd.concat([
pd.concat([df1], axis = 1),
pd.concat([df2], axis = 1)]).to_csv("Output.csv", header = True, encoding = 'utf-8')
這是輸出,索引是日期:
0 100 200 300
05/03/2017 ND ND ND ND
06/03/2017 ND ND ND ND
07/03/2017 36 36.2 36.2 21.3
08/03/2017 35.5 35.5 59.8 35.9
09/03/2017 35.7 43.6 35.2 35.2
10/03/2017 ND ND ND ND
6 - - - -
7 0 100 200 300
05/03/2017 ND ND ND ND
06/03/2017 ND ND ND ND
07/03/2017 1.4 0.2 0.3 0.3
我希望索引6和7像索引0一樣是空的。但我無法確定它是如何不像索引0那樣工作。
我想你需要rename
:
df3 = df3.rename(index={6:'', 7:''})
print (df3)
0 100 200 300
05/03/2017 ND ND ND ND
06/03/2017 ND ND ND ND
07/03/2017 36 36.2 36.2 21.3
08/03/2017 35.5 35.5 59.8 35.9
09/03/2017 35.7 43.6 35.2 35.2
10/03/2017 ND ND ND ND
- - - -
0 100 200 300
05/03/2017 ND ND ND ND
06/03/2017 ND ND ND ND
07/03/2017 1.4 0.2 0.3 0.3
更動態的解決方案 - 獲取len(df1.index)
更快的len(df1)
到變量idx1
,然后只需要一個concat
並最后rename
為idx1
:
spaceRow1 = "-"
idx1 = len(df1.index)
df1.loc[idx1] = spaceRow1
spaceRow2 = L
df1.loc[idx1+1] = spaceRow2
df3 = pd.concat([df1, df2])
df3 = df3.rename(index={idx1:'', idx1+1:''})
print (df3)
0 100 200 300
05/03/2017 ND ND ND ND
06/03/2017 ND ND ND ND
07/03/2017 36 36.2 36.2 21.3
08/03/2017 35.5 35.5 59.8 35.9
09/03/2017 35.7 43.6 35.2 35.2
10/03/2017 ND ND ND ND
- - - -
0 100 200 300
05/03/2017 ND ND ND ND
06/03/2017 ND ND ND ND
07/03/2017 1.4 0.2 0.3 0.3
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.