简体   繁体   中英

How can I extract from a list of pandas dataframes, a specific column?

I created a list of dataframes by iterating through multiple csv files and appending these to a list, like so:

files = "foldername/*.csv"
content = glob.glob(files)

dataframes = []
for file in content:
    dataframes.append(pd.read_csv(file))

Current output : a list containing all these dataframes. (I will not display the list here because it is very large).

I would like to extract column labelled as '3' of every listed dataframe. Each dataframe looks like this (this is the first dataframe of the list:

[image of dataframe has been deleted, sorry for inconvenience]

Desired output : Accessing column '3' of each dataframe.

Current (unsuccessful) code : Here I am just trying to see if I can print column '3' of each listed dataframe.

for df in dataframes:
    print(df['3'])

As first remark, I think you should pass index_col=0 to pd.read_csv . Regarding accessing the column 3 , this may be a number, thus the following should work df[3] or df.loc[:,3]

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