You can select one column by doing df[column_name]
, such as df['age']
, or multiple columns as df[[column_name1, column_name2]]
. For a single column, you can also select it using the attribute syntax, df.<column_name>
, as in, df.age
. Note, a single column in Pandas is called a Series and operates differently from a DataFrame. Later exercises will explore the difference.
Suppose you constructed a DataFrame by
import pandas as pd df = pd.DataFrame({'name': ['Jeff', 'Esha', 'Jia'], 'age': [30, 56, 8]})
Giving you the DataFrame
name | age | |
---|---|---|
1 | Jeff | 30 |
2 | Esha | 56 |
3 | Jia | 8 |
Now, you want to select just the name column from the DataFrame.
Complete the function, name_column(df)
, by having it return only the name column.
df = pd.DataFrame({'name': ['Jeff', 'Esha', 'Jia'], 'age': [30, 56, 8]})
name | age | |
---|---|---|
0 | Jeff | 30 |
1 | Esha | 56 |
2 | Jia | 8 |
name | |
---|---|
0 | Jeff |
1 | Esha |
2 | Jia |