I have data in different columns but I don\'t know how to extract it to save it in another variable.
index a b c
1 2 3 4
2 3 4 5
You can use pandas. I create the DataFrame:
import pandas as pd
df = pd.DataFrame([[1, 2,5], [5,4, 5], [7,7, 8], [7,6,9]],
index=['Jane', 'Peter','Alex','Ann'],
columns=['Test_1', 'Test_2', 'Test_3'])
The DataFrame:
Test_1 Test_2 Test_3
Jane 1 2 5
Peter 5 4 5
Alex 7 7 8
Ann 7 6 9
To select 1 or more columns by name:
df[['Test_1','Test_3']]
Test_1 Test_3
Jane 1 5
Peter 5 5
Alex 7 8
Ann 7 9
You can also use:
df.Test_2
And yo get column Test_2
Jane 2
Peter 4
Alex 7
Ann 6
You can also select columns and rows from these rows using .loc()
. This is called "slicing". Notice that I take from column Test_1
to Test_3
df.loc[:,'Test_1':'Test_3']
The "Slice" is:
Test_1 Test_2 Test_3
Jane 1 2 5
Peter 5 4 5
Alex 7 7 8
Ann 7 6 9
And if you just want Peter
and Ann
from columns Test_1
and Test_3
:
df.loc[['Peter', 'Ann'],['Test_1','Test_3']]
You get:
Test_1 Test_3
Peter 5 5
Ann 7 9