Cannot Add Data Frames to List

Asked 1 months ago, Updated 1 months ago, 3 views

Python 3.7.6.
I'm trying to add data frames to the list, but it doesn't work because it's judged to be str type.
The results are as follows:
files is a list that contains the file path to be read by the func function.

lst=list()
for i in files:
    lst.append(func(k=files[i]))

TypeError: list indications must be integrators or slices, not str

Looking for str, it looks like a data frame.

In[10]—type(func(k=files[i]))
Out [10]—pandas.core.frame.DataFrame

Is it impossible to append the data frame to the list?
I would appreciate it if you could reply to me if you know more.

Thank you for your cooperation.

python pandas

2022-09-30 13:51

1 Answers

Why don't you make it into a two-dimensional array?
For example, the dataframe name is name_df.
First, retrieve the dataframe column.

files=list(name_df.columns)

Then use the columns to store dataaframe elements in the array.

 lis=[ ]
for w, i in enumerate (files):
  lis.append([])
  lis[w].append(name_df[i])      

Can I do what I want with this?
I don't really understand what I want to do, so it may be a vague answer.
Try it


2022-09-30 13:51

If you have any answers or tips


© 2022 OneMinuteCode. All rights reserved.