In Python Pandas DataFrame there are many in-built methods that help in performing different types of tasks on given data. There are many methods that we can use to select columns in pandas dataframe. Therefore here are some methods:
1. Selecting a single column
If you want to select the first column 'physics', you can pass the column name as a string to the indexing operator.
import pandas as pd
physics = [88,98,67]
chemistry = [56,78,90]
biology = [85,69,93]
students = pd.DataFrame({'Physics':physics,'Chemistry':chemistry,'Biology':biology})
students['Physics'].head()
Output
0 88
1 98
2 67
Name: Physics, dtype: int64
2. Selecting multiple columns
If you want to select multiple columns you can pass a list of columns names as indexing operators.
subject = students[['Physics','Biology']]
subject.head()
Output
Physics | Biology | |
---|---|---|
0 | 88 | 85 |
1 | 98 | 69 |
2 | 67 | 93 |