Home > Enterprise >  index values always gets displayed even on setting .to_string(index=False) in Pandas
index values always gets displayed even on setting .to_string(index=False) in Pandas

Time:01-27

I have a table like this Input Table. Trying to group by year and display the data. Here is the code.

import pandas as pd
data=pd.read_excel("Book6.xlsx",sheet_name="Sheet6")

df_new = data[['Date1','Name', 'Fruit','Price']]
df_new['Date1'] = pd.to_datetime(df_new['Date1'], dayfirst=True, errors='coerce')
result = df_new.reset_index().groupby([df_new['Date1'].dt.year,df_new['Name'],df_new['Fruit'],df_new['Price']]).agg('sum')
print(result)#.to_string(index=False))

Even on setting the index=False in .to_string, still the index getting displayed. Here is the output table. I dont require the index to be displayed.

Output Table

CodePudding user response:

No need for pandas.DataFrame.to_string, just add drop=True to pandas.DataFrame.reset_index :

result = (df_new.reset_index(drop=True)
                .groupby([df_new['Date1'].dt.year,df_new['Name'],df_new['Fruit'],df_new['Price']])
                 .agg('sum'))

Output :

print(result)

                          Price
Date1 Name Fruit Price       
2017  ghi  Cat   100      200
2018  abc  Ball  20        40
2019  abc  Apple 25        75
      ghi  Apple 25        50
2020  def  Apple 25        50
           Cat   100      200
      ghi  Ball  20        40
2021  abc  Apple 25        50
  • Related