sajjad ahmad
sajjad ahmad

Reputation: 7

Read excel and get data of 1 row as a object

I want to read a excel file using pandas and want row of the excel as object like

{2, 3,'test data' , 1}

I am reading pandas file like

excel_data = pd.read_excel(upload_file_url , index_col=None, header=None)
for name in excel_data:
    print(name)

but on printing I am getting just plan text . how I can achieve this with pandas ?

Upvotes: 0

Views: 5672

Answers (1)

Dima Chubarov
Dima Chubarov

Reputation: 17159

The iterrows() method might help to get individual rows from the dataframe.

Consider the following crude solution

excel_data = pd.read_excel(upload_file_url , index_col=None, header=None)
for name in excel_data.iterrows():
    print(str(name[1].tolist()).replace("[","{").replace("]","}"))

Here we use iterrows() to get individual rows of the spreadsheet as tuples. Each tuple contains the row data at position 1 as Pandas Series.

In order to convert Pandas Series to {2, 3,'test data' , 1} you might just convert it to list and replace square brackets with curly brackets.

Update: If you could print the data as dict instead of a list in curly brackets, the code would be simplified.

excel_data = pd.read_excel(upload_file_url , index_col=None, header=None)
for name in excel_data.iterrows():
    print(name[1].to_dict())

Upvotes: 2

Related Questions