Automatically clean pandas dataframe column names: R. Burke Squires: NIAID Bioinformatics and Computational Biosciences Branch: OK, let's get started by importing the pandas library. You can export a file into a csv file in any modern office suite including Google Sheets. The pandas read_csv() function is used to read a CSV file into a dataframe. Get DataFrame Column Names. You can access the column names using index. Example 1: Print DataFrame Column Names. pd.read_csv(file_name, index_col= 0) usecols. pandas.read_csv ¶ pandas.read_csv ... Row number(s) to use as the column names, and the start of the data. import pandas emp_df = pandas.read_csv('employees.csv') print(emp_df) Output: Emp ID Emp Name Emp Role 0 1 Pankaj Kumar Admin 1 2 David Lee Editor 2 3 Lisa Ray Author Let us see how to read specific columns of a CSV file using Pandas. In this example, we get the dataframe column names and print them. Read CSV file in Pandas as Data Frame pandas read_csv method of pandas will read the data from a comma-separated values file having .csv as a pandas data-frame. Therefore, if no column names are specified, default behavior of csv file is to take header=0 and column names are inferred from the ,first line of the file. Use the following csv data as an example. Now for the second code, I took advantage of some of the parameters available for pandas.read_csv() header & names. To get the names of the data frame rows: >>> df.index Index(['Alice', 'Bob', 'Emma'], dtype='object') Get the row names of a pandas data frame (Exemple 2) Another example using the csv file train.csv (that can be downloaded on kaggle): >>> import pandas as pd >>> df = pd.read_csv('train.csv') >>> df.index RangeIndex(start=0, stop=1460, step=1) : Sell) or using their column index (Ex. The pandas function read_csv() reads in values, where the delimiter is a comma character. Let’s see how to read it into a DataFrame using Pandas read_csv() function. Read CSV Read csv with Python. ... index_col – This defines the names of row labels, it can be a column from the data or the list of integer or string, None by default. : 0). You can access the column names of DataFrame using columns property. Python Program We will use Pandas coliumns function get the names of the columns. However, having the column names as a list is useful in many situation. df = pd.read_csv(file_name, usecols = [0,1,2]) Pandas returns the names of columns as Pandas Index object. It comes with a number of different parameters to customize how you’d like to read the file. It is the basic object storing axis labels. We will pass the first parameter as the CSV file and the second parameter the list of specific columns in the keyword usecols.It will return the data of the CSV file of specific columns. This can be done with the help of the pandas.read_csv() method. The Example. The following is the general syntax for loading a csv file to a dataframe: import pandas as pd df = pd.read_csv(path_to_file) In this post, we will first see how to extract the names of columns from a dataframe. It returns an object. How to read csv files in python using pandas? Related course: Data Analysis with Python Pandas. Here are two approaches to get a list of all the column names in Pandas DataFrame: First approach: my_list = list(df) Second approach: my_list = df.columns.values.tolist() Later you’ll also see which approach is the fastest to use. If header=None , column names are assigned as integer indices and first line of the file is read as first row of the DataFrame: df = pd.read_csv("SampleDataset.csv", header=None) df.head() You have two options on how you can pull in the columns – either through a list of their names (Ex. DataFrame.columns. When you want to only pull in a limited amount of columns, usecols is the function for you.