Reputation: 676
I read an excel file and save each tab as a pandas dataframe.
import pandas as pd
xla = pd.ExcelFile("file_name.xlsx")
kl=xla.sheet_names
hf_list=[]
for i in range(len(kl)):
hf_list.append(pd.read_excel(xla, i,index_col=0))
I intend to compute rank of each dataframe in the list so have written the following code.
def score_card(raw_list):
score_list=[]
for i in range(len(raw_list)):
score_list.append(raw_list[i].rank(axis=1))
return score_list
score_list=score_card(hf_list)
I was wondering if there is a way to vectorize the code and avoid for loop(s) in the score_card function (and also reading the excel file). Thanks in advance for your time.
Upvotes: 2
Views: 183
Reputation: 863291
If use parameter sheet_name = None
in read_excel
get orderdict of DataFrames for each sheetname:
dfs = pd.read_excel("file_name.xlsx", sheet_name = None, index_col=0)
Then use list comprehension:
score_list = [v.rank(axis=1) for k, v in dfs.items()]
Or create big DataFrame with concat
:
df = pd.concat(dfs.values())
Upvotes: 2