Dmitry
Dmitry

Reputation: 195

Slicing a list of pandas dataframes

I have a list containing a number of dataframes. Each dataframe contains two columns: timestamp and data. I would like to extract only the second column from each dataframe from the list. Ideally, I would prefer to not use column names but column indexes.

Example dataframe:

months = [            Timestamp   Data
0 2019-10-23 14:00:00  24249.79243
1 2019-10-24 14:00:00  22141.10212
2 2019-10-25 14:00:00  21043.27311
3 2019-10-26 14:00:00  21663.70388
4 2019-10-27 14:00:00  22980.65174
5 2019-10-28 14:00:00  21767.26619
6 2019-10-29 14:00:00  22464.75061
7 2019-10-30 14:00:00  22839.18973
8 2019-10-31 14:00:00  22472.66895, 
             Timestamp    Data
0  2019-11-01 14:00:00  23062.698230
1  2019-11-02 14:00:00  22493.173590
2  2019-11-03 14:00:00  22844.434790
3  2019-11-04 14:00:00  23510.446750
4  2019-11-05 14:00:00  23575.067920
5  2019-11-06 14:00:00  23587.874350
6  2019-11-07 14:00:00  23543.441200
7  2019-11-08 14:00:00  22037.511610
8  2019-11-09 14:00:00  19832.109730]

I would like to get list of data columns only. I tried something like maonths[:][1] but it obviously did not work. What would be the way to reduce this list to the list of 2nd dataframe columns only?

Thanks!

Upvotes: 0

Views: 659

Answers (2)

jezrael
jezrael

Reputation: 863266

If want list of Seriess use list comrehension with DataFrame.iloc:

L = [x.iloc[:, 1] for x in months]

If want list of DataFrames, one column, add []:

L = [x.iloc[:, [1]] for x in months]

Loop solution instead list comprehension is:

L = []
for x in months:
     L.append(x.iloc[:, 1])

Upvotes: 1

shaik moeed
shaik moeed

Reputation: 5785

Try this,

for df in months:
    print(df.iloc[:,1]) # second column, to apply your logic

Upvotes: 1

Related Questions