JamesHudson81
JamesHudson81

Reputation: 2273

separate dataframe into individual columns with its own index

I have the following df:

                   Arrows :Current    Arrows:previous 1M    Arrows:previous 2M
silverine               1                0                    0
fire                    2                2                    2
ice                    11               12                   10
wind                    4                4                    4
poisonous               2                2                    9

Supposing that I do not know how many columns I have I would like to obtain each column independiently with the index.

The desired output would look something like this:

                   Arrows :Current 
silverine               1         
fire                    2         
ice                    11            
wind                    4                
poisonous               2              


                   Arrows:previous 1M 
silverine               0  
fire                    2         
ice                    12           
wind                    4           
poisonous               2

                  Arrows:previous 2M
silverine                0
fire                     2
ice                     10
wind                     4
poisonous                9

Is there any defined property in pandas to obtain each column with the index ?

Upvotes: 1

Views: 51

Answers (1)

jezrael
jezrael

Reputation: 862731

You need select data by columns to Series, what is each column of DataFrame with same index as df.

If need list of all Series:

L = [df[col] for col in df.columns]
print (L)
[silverine     1
fire          2
ice          11
wind          4
poisonous     2
Name: Arrows :Current, dtype: int64, silverine     0
fire          2
ice          12
wind          4
poisonous     2
Name: Arrows:previous 1M, dtype: int64, silverine     0
fire          2
ice          10
wind          4
poisonous     9
Name: Arrows:previous 2M, dtype: int64]

It need only loop by all columns:

for col in df.columns:
    print (df[col])
silverine     1
fire          2
ice          11
wind          4
poisonous     2
Name: Arrows :Current, dtype: int64
silverine     0
fire          2
ice          12
wind          4
poisonous     2
Name: Arrows:previous 1M, dtype: int64
silverine     0
fire          2
ice          10
wind          4
poisonous     9
Name: Arrows:previous 2M, dtype: int64

Upvotes: 1

Related Questions