Pandas read_table() function
Pandas is one of the most used packages for analyzing data, data exploration, and manipulation. While analyzing real-world data, we often use the URLs to perform different operations and Pandas provide multiple methods to read tabular data in Pandas. One of those methods is read_table()
Pandas read_table() Function Syntax
Below is the syntax of pandas.read_table() function.
Syntax: pandas.read_table(filepath_or_buffer, delimiter=None, header=’infer’, names=None, index_col=None, usecols=None, dtype=None, skiprows=None, na_values=None, parse_dates=False, encoding=None, comment=None)
Parameters:
f
ilepath_or_buffer
: The path or object representing a file to be read.delimiter
: The delimiter to use, in this case, it’s retained for specifying the column separator.header
: Row(s) to use as the column names.names
: List of column names to use.index_col
: Column(s) to set as index (can be a single column name or a list of names).usecols
: Columns to read from the file.dtype
: Data type to force.skiprows
: Number of rows to skip at the beginning of the file.na_values
: Additional strings to recognize as NA/NaN.Returns: A comma(‘,’) separated values file(csv) is returned as two dimensional data with labelled axes.
To get the link to csv file used in the article, click here.
Python Pandas read_table() Function Examples
Below are some examples of Pandas read_table() function in Python:
Example 1: Pandas Read CSV into DataFrame
Display the whole content of the file with columns separated by ‘,’. In this example, the code utilizes the pandas library to read data from a CSV file named ‘nba.csv’ using the pd.read_table
function with a comma (,
) as the specified delimiter. The resulting DataFrame is not assigned to a variable for further manipulation. Here, we will read tabular data in pandas by using read_table().
Python3
# importing pandas import pandas as pd pd.read_table( 'nba.csv' , delimiter = ',' ) |
Output:
Example 2: Skipping rows Without Indexing Using read_table() Function
In this example, the code employs the pandas library to read data from a CSV file (‘nba.csv’) using pd.read_table
, specifying a comma (,
) as the delimiter. It skips the first 4 rows and designates the values in the first column as the DataFrame index and we will read tabular data in pandas.
Python3
# importing pandas import pandas as pd pd.read_table( 'nba.csv' , delimiter = ',' , skiprows = 4 , index_col = 0 ) |
Output:
In the above code, four rows are skipped and the last skipped row is displayed.
Example 3: Skipping Rows With Indexing in Pandas
In this example, the code utilizes pandas to read tabular data in Pandas from the ‘nba.csv’ file with a comma (,
) as the delimiter. It skips the first 4 rows, creating a DataFrame with the remaining data.
Python3
# importing pandas import pandas as pd pd.read_table( 'nba.csv' , delimiter = ',' , skiprows = 4 ) |
Output:
Example 4: Read Specific Rows Using read_table()
In case of large file, if you want to read only few lines then give required number of lines to nrows. In this example, the code employs pandas to read the first 4 rows from the ‘nba.csv’ file, using a comma (,
) as the delimiter. It designates the values in the first column as the DataFrame index.
Python3
# importing pandas import pandas as pd pd.read_table( 'nba.csv' , delimiter = ',' , index_col = 0 , nrows = 4 ) |
Output:
Example 5: Skipping Lines from Bottom of File
If you want to skip lines from bottom of file then give required number of lines to skipfooter. In this example, the code uses pandas to read data from ‘nba.csv’, specifying a comma (,
) as the delimiter. It sets the values in the first column as the DataFrame index, employs the Python engine, and skips the last 5 rows during the file reading process.
Python3
# importing pandas import pandas as pd pd.read_table( 'nba.csv' , delimiter = ',' , index_col = 0 , engine = 'python' , skipfooter = 5 ) |
Output:
Example 6: Multi-Level Indexing with Pandas
Row number(s) to use as the column names, and the start of the data occurs after the last row number given in header.
Python3
# importing pandas import pandas as pd pd.read_table( 'nba.csv' , delimiter = ',' , index_col = 0 , header = [ 1 , 3 , 5 ]) |
Output: