. Advertisement .
..3..
. Advertisement .
..4..
We might need to retrieve the column names while examining real datasets, which are frequently quite large in size, in order to carry out specific actions.
Let us introduce to you a brand new topic regarding pandas get column names from dataframe.
Dataframe Example
First, let’s employ the nba.csv file to build up a straightforward dataframe.
The columns in our DataFrame will be named as Teachers, Fee, Prolongation, and Discount.
# Create a pandas DataFrame.
import pandas as pd
technologies = ({
'Teachers':["Simmon","Pinmark","Hanson","Putin","Plex","Hanson","Simmon","Putin"],
'Fee' :[32000,45000,33000,44000,36000,45000,35000,42000],
'Prolongation':['50days','30days','40days','35days','35days','60days','50days','55days'],
'Discount':[2000,1300,2000,1100,2500,2300,1200,1500]
})
df = pd.DataFrame(englishteaching, columns=['Teachers','Fee','Prolongation','Discount'])
print(df)
That way, we we achieve the below output:
Teachers Fee Prolongation Discount
0 Simmon 32000 50days 2000
1 Pinmark 45000 30days 1300
2 Hanson 33000 40days 2000
3 Putin 44000 35days 1100
4 Plex 36000 35days 2500
5 Hanson 45000 60days 2300
6 Simmon 35000 50days 1200
7 Putin 42000 55days 1500
Pandas Get Column Names from DataFrame
Method #1: Utilizing Columns
The code snippet below may be used to retrieve the column names from a pandas dataframe. As such, the dataframe’s column labels will be returned via the columns property.
Snippet:
df.columns
Output:
Index(['Teachers', 'Fee ', 'Prolongation', 'Discount'],
dtype='object')
Just like that, your other pandas dataframe’s column headers may be obtained in exactly the same way.
Method #2: Iterating over columns
Running the code:
# Import pandas package
import pandas as pd
# making data frame
data = pd.read_csv("nba.csv")
# iterating the columns
for col in data.columns:
print(col)
Output:
Teachers
Fee
Prolongation
Discount
Method #3: Making Use Of The Columns Property With A Dataframe Object
Running the code:
# Import pandas package
import pandas as pd
# making data frame
data = pd.read_csv("nba.csv")
# list(data) or
list(data.columns)
Output:
[‘Teachers’,
‘Fee’,
‘Prolongation’,
‘Discount’,]
Method #4: Utilizing column.values method to give back the index’ array.
Running the code:
# Import pandas package
import pandas as pd
# making data frame
data = pd.read_csv("nba.csv")
list(data.columns.values)
Output:
[‘Teachers’,
‘Fee’,
‘Prolongation’,
‘Discount’,]
Method #5: Using the tolist() function and the supplied values for the given list of columns.
Running the code:
# Import pandas package
import pandas as pd
# making data frame
data = pd.read_csv("nba.csv")
list(data.columns.values.tolist())
Output:
[‘Teachers’,
‘Fee’,
‘Prolongation’,
‘Discount’,]
Method #6: Utilizing the sorted() function to return The list of columns alphabetically arranged.
Running the code:
# Import pandas package
import pandas as pd
# making data frame
data = pd.read_csv("nba.csv")
# using sorted() method
sorted(data)
Output:
[‘Discount’,
‘Fee’,
‘Prolongation’,
‘Teachers’]
The Bottom Line
About are neatly the most feasible methods regarding the “pandas get column names from dataframe” topic. Hopefully, this article will be of great aid to you. See then!
Leave a comment