4

I have a CSV file that I used to get its values from a columns but my problem is the value I'm getting is from the two headers which is Rounded-Download-Speed and Rounded-Upload-Speed so what I did is separate them and concatenate it.

Then I'm adding a two row to separate them inside but the problem is the index of the row displays but what I want for that is to be a blank cell. Here's my code:

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')

Here's the output, the index is the date:

            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

I want the index 6 & 7 to be empty just like the index 0. But I can't figure how it's not working like the index 0.

1 Answer 1

1

I think you need 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

More dynamic solution - get len(df1.index) what is faster as len(df1) to variable idx1, then need only one concat and last 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.3
Sign up to request clarification or add additional context in comments.

1 Comment

100% working!! Thank you so much! Easy to understand :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.