How to Read Csv From Absolute Path Python Pandas

A comma-separated values (csv) file is widely used for storing data. Understanding how to read or import them in python is very crucial for whatsoever data scientist, analysts or anyone who works with data.

In this post you volition larn –

A. What is a csv file ?

B. How to read a csv file in pandas?

C. Reading files with different separators.

D. Rename cavalcade names when reading a file.

E. Reading only few columns from a file.

F. Select only N rows when reading a file.

One thousand. Setting columns as Index in read_csv.

H. Parsing Engagement columns when reading a file.


A. What is a csv file?

A comma-separated values (csv) file is a delimited text file that uses a comma to separate values. Each line of the file is a data record. Each record consists of one or more than fields, separated by commas. The use of the comma as a field separator is the source of the name for this file format – Wikipedia.

B. How to read a csv file in pandas ?

To read a csv file in pandas, nosotros use the pandas read_csv office. Permit's get some hands on practice in reading a csv file together.

Download files –

1 . Google Drive link.

2 . GitHub Link.

Before nosotros read a csv file, first nosotros take to import the pandas library.

            # import pandas import pandas as pd          

1 . Reading files locally from a estimator –

When reading a file locally, you lot have to provide the file_path + file_name to the pandas read_csv function. You can either utilize a relative path or you can utilise an accented path on Mac, Windows, and Linux.

Reading a csv file with relative path –

            # read csv file using relative path on Mac store_sales = pd.read_csv('../data/clothing_store_sales.csv') store_sales.head()          

Reading a csv file with absolute path –

            # read csv file using absolute path on Windows store_sales = pd.read_csv("D:\workspace\lwd\information\clothing_store_sales.csv") store_sales.head()          

two. Reading files remotely from internet –

You lot tin can likewise read a csv or any other format file in pandas from internet. All yous have to do is provide the file url to pandas read_csv role.

            # read csv file remotely from internet url="https://raw.githubusercontent.com/bprasad26/lwd/main/data/clothing_store_sales.csv"  store_sales = pd.read_csv(url) store_sales.head()          

C. Reading files with unlike separators –

By default read_csv function will read a comma-separated file but If you desire, you lot tin also uses other separators like semicolon (;), a tab (\t), a space ( ) and a pipe (|).

Allow's read the Gap minder data which is tab separated.

            # read a tab separated file gap_minder = pd.read_csv('../data/gapminder.tsv', sep='\t') gap_minder.head()          

D. Rename column names when reading a file –

When you read a file, yous can too rename the column names using the name parameter of read_csv office.

            # new column names cols = ['state','continent','year','life_exp','pop','gdp']  gap_minder = pd.read_csv('../data/gapminder.tsv', names=cols, sep='\t') gap_minder.head()          

And if you look at the above upshot. Yous can see that the former column names are being added as a row in the dataframe. To avoid this you lot accept to set up the header parameter.

            # new column names cols = ['country','continent','yr','life_exp','pop','gdp']  gap_minder = pd.read_csv('../data/gapminder.tsv', header=0, names=cols, sep='\t') gap_minder.caput()          

Related post – How to Rename Column names.

Due east. Reading only few columns from a file –

Sometimes when y'all read a file, you lot don't want to read all of the columns. You just desire to read a few of them. In that case you can apply the usecols parameter of read_csv function.

Let's read a different version of clothing sales data that contains some useless columns.

            sales = pd.read_csv('../data/cs_corrupted.csv') sales.head()          

You tin can see that we accept some useless 'unnamed' cavalcade. We can exclude these column when reading a file past but specifying column names in a list which nosotros want to read using the usecols parameter and rest of the columns that are non in the listing will exist ignored past pandas.

            # just read selected columns cols_to_read = ['Client', 'Type of Customer', 'Items', 'Net Sales',        'Method of Payment', 'Gender', 'Marital Status', 'Historic period']  sales = pd.read_csv('../data/cs_corrupted.csv', usecols=cols_to_read) sales.caput()          

F. Select only N rows when reading a file –

Sometimes you may want to read simply few rows of data from a file rather than the whole file. You tin do this using the nrows parameter.

            # read only 10 rows of data gap_minder = pd.read_csv('../information/gapminder.tsv', nrows=ten, sep='\t') gap_minder          

One thousand. Setting columns as Index in read_csv –

Whenever you read a file in pandas, past default it adds an alphabetize for you from 0 to north-1. If y'all want you lot can set any columns as a index using the index_col parameter.

1 . Setting one cavalcade as Index –

            # set country column every bit index  gap_minder = pd.read_csv('../data/gapminder.tsv', index_col='state', sep='\t') gap_minder.head()          

2. Setting Multiple columns as Index –

To gear up multiple columns as index, just laissez passer the column names in a list to the index_col parameter.

            # set multiple columns as alphabetize  gap_minder = pd.read_csv('../data/gapminder.tsv',                           index_col=['continent','state'],                           sep='\t') gap_minder.caput()          

H. Parsing Date columns when reading a file –

When yous read a file which contains date information, pandas may read them as cord object compared to datetime type. If you want to parse these columns as a datetime type, you tin use the parse_dates parameter.

            # parsing date columns as datetime stock = pd.read_csv('../data/tesla_stock_prices.csv', parse_dates=['Date']) stock.head()          

In that location are many other options in pandas read_csv function, delight bank check the document to learn more – Pandas read_csv doc.

Related Post – Pandas to_csv – write a dataframe to a csv file.

If you like this postal service then please share information technology with others and subscribe to our blog below.

brownfitionly.blogspot.com

Source: https://lifewithdata.com/2021/12/10/pandas-read_csv-read-a-csv-file-in-python/

0 Response to "How to Read Csv From Absolute Path Python Pandas"

Postar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel