Tuesday, 10 October 2023

How to Select a Column in Pandas DataFrame?

There are two ways to select a column from a DataFrame

a.   Using . notation

b.   Using [] notation

 

Using . notation

Syntax

dataframe.columnName

 

Example

name_column = df.Name

 

Return type of name_column is a Series, you can confirm the same with the statement ‘type(name_column)’.

 

dataframe_column_select.py

import pandas as pd

# Create a sample DataFrame
data = {'Name': ['Krishna', 'Ram', 'Joel', 'Gopi', 'Jitendra', "Raj"],
        'Age': [34, 25, 29, 41, 52, 23],
        'My City': ['Bangalore', 'Chennai', 'Hyderabad', 'Hyderabad', 'Bangalore', 'Chennai']}

df = pd.DataFrame(data)
print(df)

print('\nPrint Name column details')
name_column = df.Name
print(name_column)
print("\ntype : ", type(name_column))

Output

       Name  Age    My City
0   Krishna   34  Bangalore
1       Ram   25    Chennai
2      Joel   29  Hyderabad
3      Gopi   41  Hyderabad
4  Jitendra   52  Bangalore
5       Raj   23    Chennai

Print Name column details
0     Krishna
1         Ram
2        Joel
3        Gopi
4    Jitendra
5         Raj
Name: Name, dtype: object

type :  <class 'pandas.core.series.Series'>

Drawback of . notation

If the column name has any spaces in it, we can’t access the column using . notation. For example, we can’t access the column ‘My City’ here.

 

Using [] notation

Syntax

Dataframe['columnName']

 

Example

my_city_column = df['My City']

 

dataframe_column_select_arrow_notation.py

import pandas as pd

# Create a sample DataFrame
data = {'Name': ['Krishna', 'Ram', 'Joel', 'Gopi', 'Jitendra', "Raj"],
        'Age': [34, 25, 29, 41, 52, 23],
        'My City': ['Bangalore', 'Chennai', 'Hyderabad', 'Hyderabad', 'Bangalore', 'Chennai']}

df = pd.DataFrame(data)
print(df)

print('\nPrint "My City" column details')
my_city_column = df['My City']
print(my_city_column)

Output

       Name  Age    My City
0   Krishna   34  Bangalore
1       Ram   25    Chennai
2      Joel   29  Hyderabad
3      Gopi   41  Hyderabad
4  Jitendra   52  Bangalore
5       Raj   23    Chennai

Print "My City" column details
0    Bangalore
1      Chennai
2    Hyderabad
3    Hyderabad
4    Bangalore
5      Chennai
Name: My City, dtype: object

 

Previous                                                 Next                                                 Home

No comments:

Post a Comment