How to Read a Particular Column From Csv File in Python

Having issues with on how to read specific columns from csv in python pandas? We take solution for you. Many people who are working with CSVs with a lot of columns, face difficulties to find an piece of cake way to read in only the columns one needs. You can e'er pre-filter the data using cutting or awk on command line. But a simple and easy tool for this is the Pandas. There are a few ways in which you can read in simply the columns you need.

Firstly, y'all can filter data when you are reading information technology with the command read_csv. Secondly, y'all tin read the whole affair then select simply the columns you need. The method of filtering with read_csv is a better fashion and we can evidence that. So, here we get:

Also Read: Invoice2data Python Library: Introduction and Setup

What is a CSV File?

ACSV file (Comma Separated Valuesfile) is a blazon of patently textfile that uses specific structuring to accommodate tabular data. Because it'south a obviously textfile, it tin can comprise only bodily text data—in other words, printable ASCII or Unicode characters. The structure of aCSV file is given away by its name. It contains structural data in a particular format in the form of columns and rows.  A new line terminates each row to start the side by side row. Similarly, a comma, besides known equally the delimiter, separates columns within each row.

Giving yous a instance:

Read specific columns from csv in python pandas

Now that you accept understood a little bit virtually CSV. It is fourth dimension nosotros should talk about Pandas and how to read the csv files in your python.

Installing Pandas !!

We have to install Panda before using the framework. Ane of the easiest methods to install Pandas is to install Anaconda. It is a cross-platform Python Distribution for tasks similar Python computing and data assay. Once you install Anaconda, you will have access to Pandas and other libraries such equally SciPy and NumPy without doing annihilation else.

pip install pandas

Reading CSV Files with read_csv() !!

Now when we are through with installing process we are going to tell you. How to read csv in python. Nosotros read a particular csv in python to show you an instance.

import pandas as pd

Now re-create and paste the following code to parse the file in your python.

Python_OL_data = pd.read_csv('Python_OL.csv')

When nosotros execute this code, it will read the CSV file "Python_OL.csv" from the current directory. You lot can run across from the script higher up that to read a CSV file, you have to laissez passer the file path to theread_csv() method of the Pandas library. Theread_csv() method then returns a Pandas DataFrame that contains the information of the CSV file.

Y'all can also read the first five columns of the csv by default by writing head . This is the method of the pandas DataFrame .

Python_OL_data.head()
Read specific columns from csv in python pandas

Now we are washed with basics of csv and how to read the CSV and how to cheque the meridian values. We are going to talk well-nigh Reading specific columns from csv in python pandas.

Read specific columns from csv in python pandas

To read a specific column into csv. Yous take to empathize a piddling bit well-nigh iloc function which helps in getting the integer position based (from0 tolength-one of the axis), but may also be used with a boolean array.

The allowed inputs in the .iloc feature are:

  • An integer, e.g.five.
  • A listing or array of integers, e.yard.[four, 3, 0].
  • A slice object with ints, e.g.1:7.
  • A boolean array.

We are providing example with the iloc feature to requite you an idea of how to read specific columns from csv in python pandas.

import pandas equally pd dataset=pd.read_csv("C://Users//harsh//Desktop//uppu//Python_OL.csv") impress("Column Name: "+dataset.columns[one]) value=dataset.iloc[:,ane:two].values impress("Value") impress(value) dataset.head()

The impress prints the Column proper noun and .iloc selects the columns which yous want to read and .dataset.iloc[:, 1:2].values, it's gives a2 dimensional(matrix). And selects the columns after skipping i and picks the second 1.

The output of reading a specific column with the help of .iloc.

Read specific columns from csv in python pandas

Conclusion:

Then, as we can encounter, filtering for the columns that we demand using the .iloc param inread_csv is nigh 4 times faster and uses almost half the retentivity in this test. There also doesn't seem to be a big loss of performance between using thedf.loc[:, cols].

We have provided y'all with bones information about CSVs and how to read them. How to cheque few columns and rows. And nearly importantly How to read specific columns from csv in python pandas that tin easily understood with the example given. Thank you for reading this.

landaverdeflipper.blogspot.com

Source: https://hackanons.com/2020/10/read-specific-columns-from-csv-in-python-pandas.html

0 Response to "How to Read a Particular Column From Csv File in Python"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel