简体   繁体   中英

How to fetch very particular elements from a list?

I have a list of dataframes with different shapes, which is why I cant put the data into a 3-dimensional df or array.
Now I want to get some specific dataframes from that list in a new list now containing only the needed dfs.

list_of_df = [df1, df2, df3, ...]
index = [0,3,7,9,29,11,18,77,1009]
new_list = list_of_df[index]

the only way I can think of this is very unsexy:

new_list = []
for i in index:
   new_list.append(list_of_df[i])

is there some better solution or in general a more convenient way to store and access thousands of different dataframes?

您可以使用列表理解:

new_list = [df for (i, df) in enumerate(list_of_df) if i in index]

@Quang Hoang的答案解决了这个问题:

new_list = np.array(list_of_df)[index]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM