我有一个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那样工作。
发布于 2017-03-14 14:16:29
我觉得你需要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) to variable idx1,然后只需要一个concat,最后一个rename by 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.3https://stackoverflow.com/questions/42778552
复制相似问题