Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How remove numbering from output after extract xls file with pandas [Python]

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!

like image 814
Vinicius Donatto Avatar asked Sep 16 '19 15:09

Vinicius Donatto


People also ask

How do I write a DataFrame in excel without an index in python?

For example, we can save the dataframe as excel file without index using “index=False” as additional argument. One of the common uses in excel file is naming the excel sheet. We can name the sheet using “sheet_name” argument as shown below.

How do you drop the index of a DataFrame in python?

The most straightforward way to drop a Pandas dataframe index is to use the Pandas . reset_index() method. By default, the method will only reset the index, forcing values from 0 - len(df)-1 as the index. The method will also simply insert the dataframe index into a column in the dataframe.

How do you remove an index from a DataFrame while writing in excel?

We can remove the index column in existing dataframe by using reset_index() function. This function will reset the index and assign the index columns start with 0 to n-1. where n is the number of rows in the dataframe.


1 Answers

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

like image 95
Stoner Avatar answered Sep 25 '22 07:09

Stoner