简体   繁体   中英

Selecting dataframe columns from a list of pandas dataframes

I have a list of pandas dataframes.

list_df=[df1,df2,df3,df4]

I access dataframes by indexing the list

df1=list_df[0]
df2=list_df[1]
first_n_dfs=list_df[0:n]

Is there a simple way of selecting 'mth column' of every dataframe in the list without for loop? Currently I am using for loop.

for i in range(len(list_df)):
    print(list_df[i].iloc[:,m])

Please forgive if this is a duplicate question. I couldn't find a similar question. Thanks for your time.

If the dataframes do not have the same schema (which would allow combining them), a slight optimisation you could make is to loop over the dataframes themselves:

for df in list_df:
    print(df.iloc[:, m])

您可以使用map()来获得结果,而无需按要求使用for循环:

list(map(lambda x: x.iloc[:,m], list_df))

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