Split Data Frame into New Dataframe for each consecutive Column

Viewed 25

Looking to split columns of this data frame into multiple data frames. Each with the date column and the consecutive column. How do I get a function that can automate this. So we would have n data frames, n being the number of columns in the original data frame - 1( the date column).

enter image description here

1 Answers

The first thing first is to set the date column as the index:

df.set_index('Date')

Then, when you filter the data frame by a single column you will get a series object with the date and your column of interest:

e.g. df.P19245Y8E will give a series of the second column.

I think this will do what you need, but if you really want to create separate dataframes for each column then you just iterate through the columns:

new_dfs = []
for col in df.columns:
    new_dfs.append(df[col])

or with list comprehension:

new_dfs = [df[col] for col in df.columns]

Related