0

I collected data from several excel files with code as follows:

path_list=[list_of_paths]
data=[pd.read_excel(path) for path in path_list]

Which gave me list of dataframes Fortunately, It was only 16 files. So, I created manually separate dataframes. I wondered (and unsuccessfully searched) if there is any way to create them with some code? I tried loop like this:

i=0
for df in data:
   i+=1
   'dataframe'+str(i)=pd.DataFrame(df)

but got error 'can't assign operator.

Could somebody help me in this issue?

data_b77
  • 415
  • 6
  • 19

2 Answers2

0

There are couple things in your code:

  1. pd.read_excel(path) already gives you a data frame. So there is no point doing pd.DataFrame(df) later.

  2. 'dataframe' + str(i) is a string, which is immutable. You can't assign some value to it. You may look for some_dict['dataframe' + str(i)] = df. But what are you trying to achieve here?

Quang Hoang
  • 146,074
  • 10
  • 56
  • 74
  • Ok, but still, this will be let's say modified list of dataframe. My goal is to analyze them separately. For example fill missings (diffent in each df), rename some columns (there are different columns in some of them). How I could make this when I've got list of them and not each in separate dataframe? – data_b77 Oct 03 '19 at 08:54
  • What do you mean? Even with a list you can still do `data[i]` for `I` any number in the range. – Quang Hoang Oct 03 '19 at 11:53
0

They're already in a list, so you can access each dataframe through simple list indexing:

data[0] # dataframe1
data[1] # dataframe2

If you're persistent in separating the concerns of each dataframe, then you can assign each to its own meaningful variable:

concern_one = data[0]
# proceed to process concern_one

Though if this is the case, then I think loading them all as a group makes little practical sense. I would opt to simply analyze each one individually.

adrianp
  • 999
  • 1
  • 8
  • 13