Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
169 views
in Technique[技术] by (71.8m points)

python - Convert a set of pandas dataframes in a list

I am trying to convert a set of pandas dataframes into an unique list,

Here's what I got so far:

import pandas as pd

df1= pd.DataFrame(data={'col1': [1, 2, 5], 'col2': [3, 4, 4]})

df2 = pd.DataFrame(data={'col3':[1,2,3,4,5], 'col4':[1,2,'NA', 'NA', 'NA'], 'col5':['John', 'Mary', 'Gordon', 'Cynthia', 'Marianne']})

df3 = pd.DataFrame(data={'col6':[19, 25,20, 23]})

#### attempt to convert into a list ####
df_list = list(df1, df2, df3)

Error:

TypeError: list expected at most 1 arguments, got 3

Expected output should return the indexed dataframe name as an element of the list, something like print(df_list['df1']) would return df1 columns and rows.

Is there any way to accomplish this task?

question from:https://stackoverflow.com/questions/65891437/convert-a-set-of-pandas-dataframes-in-a-list

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It's not possible to use string indices with a list in python. Lists have numeric indices starting from 0 up to len(my_list)-1.

If you were to use the list() call itself, it requires an iterable variable:

>>> help(list)

class list(object)                                                 
 |  list() -> new empty list                                       
 |  list(iterable) -> new list initialized from iterable's items   

So you could construct a tuple and pass that to the list() class like:

>>> my_list = list((df1, df2, df3))
>>> type(my_list) 
<class 'list'>
>>> my_list[0]
... df1 outputs here ... 

But a simpler, and cleaner, way to do it is using the square brackets notation:

>>> my_list = [df1, df2, df3]
>>> type(all_dataframes)
<class 'list'>

However, if you want to use string indices, then think about using a dictionary i.e. the dict class:

>>> help(dict)

class dict(object)                                                             
 |  dict() -> new empty dictionary                                             
 |  dict(mapping) -> new dictionary initialized from a mapping object's        
 |      (key, value) pairs                                                     
 |  dict(iterable) -> new dictionary initialized as if via:                    
 |      d = {}                                                                 
 |      for k, v in iterable:                                                  
 |          d[k] = v                                                           
 |  dict(**kwargs) -> new dictionary initialized with the name=value pairs     
 |      in the keyword argument list.  For example:  dict(one=1, two=2)        
 |                                                                             
 |  Methods defined here:                                                      
 |          

Calling the dict() class directly, you'd want something like this:

>>> all_dataframes = dict(("df1", df1), ("df2", df2), ("df3", df3))
>>> type(all_dataframes)
<class 'dict'>
>>> all_dataframes["df1"]
... df1 output prints here ...

But, the simpler and clearer method would be:

>>> all_dataframes = {"df1": df1, "df2": df2, "df3": df3}
>>> type(all_dataframes)
<class 'dict'>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...