8

I have a Python Script that extracts a specific column from an Excel .xls file, but the output has a numbering next to the extracted information, so I would like to know how to format the output so that they don't appear.

My actual code is this:

for i in sys.argv:
    file_name = sys.argv[1]

workbook = pd.read_excel(file_name)
df = pd.DataFrame(workbook, columns=['NOM_LOGR_COMPLETO'])
df = df.drop_duplicates()
df = df.dropna(axis=0, how='any', thresh=None, subset=None, inplace=False)
print(df)

My current output:

1 Street Alpha <br>
2 Street Bravo

But the result I need is:

Street Alpha <br>
Street Bravo

without the numbering, just the name of the streets.

Thanks!

wjandrea
  • 28,235
  • 9
  • 60
  • 81

1 Answers1

11

I believe you want to have a dataframe without the index. Note that you cannot have a DataFrame without the indexes, they are the whole point of the DataFrame. So for your case, you can adopt:

print(df.values)

to see the dataframe without the index column. To save the output without index, use:

writer = pd.ExcelWriter("dataframe.xlsx", engine='xlsxwriter')
df.to_excel(writer, sheet_name = df, index=False)
writer.save() 

where file_name = "dataframe.xlsx" for your case.

Further references can be found at:

How to print pandas DataFrame without index

Printing a pandas dataframe without row number/index

disable index pandas data frame

Python to_excel without row names (index)?

Stoner
  • 846
  • 1
  • 10
  • 30
  • @Vinicius Donatto If you find the solution useful and it solved your problem, it will be great if you can mark the answer as accepted. :) Otherwise do update the community and we can try our best to help further. – Stoner Sep 18 '19 at 06:36
  • 2
    I combined the solution of the "Printing a pandas dataframe without row number/index" using the to_string function with your tip to print only the values ​​using "print (df.values)" setting index parameter to False and the result is very good, thanks! my output code looks like this: `print(df.to_string(index=False))` – Vinicius Donatto Sep 23 '19 at 02:16
  • 1
    @ViniciusDonatto Thanks for the update! Glad it has helped you. :) – Stoner Sep 23 '19 at 02:36
  • `index=False` from `to_excel` method did the trick on my end, thx! – marcin2x4 Feb 01 '23 at 12:07