Saturday, 11 December 2021

Python Load data from different files like CSV and Excel PYTHON

  1. Python has become a beloved language for text and file munging due to its simple syntax for interacting with files, intuitive data structures, and convenient features like tuple packing and unpacking.
  2. Pandas features a number of functions for reading tabular data as a DataFrame object.

When processing very large files or figuring out the right set of arguments to correctly processes a large file, you may only want to read in a small piece of a file or iterate through smaller chunks of the file.

Input as CSV File

  1. The csv file is a text file in which the values in the columns are separated by a comma. Let's consider the following data present in the file named input.csv.
  2. You can create this file using windows notepad by copying and pasting this data. Save the file as input.csv using the save As All files(*.*) option in notepad. or download CSV file Here

id,name,salary,start_date,dept
1,Rick,623.3,2012-01-01,IT
2,Dan,515.2,2013-09-23,Operations
3,Tusar,611,2014-11-15,IT
4,Ryan,729,2014-05-11,HR
5,Gary,843.25,2015-03-27,Finance
6,Rasmi,578,2013-05-21,IT
7,Pranab,632.8,2013-07-30,Operations
8,Guru,722.5,2014-06-17,Finance

Reading a CSV File

 
The read_csv function of the pandas library is used read the content of a CSV file into the python environment as a pandas DataFrame. The function can read the files from the OS by using proper path to the file.

import pandas as pd
data = pd.read_csv('input.csv')
print (data)

Output:


2 Reading Microsoft Excel Files


Microsoft Excel is a very widely used spread sheet program. Its user friendliness and appealing features makes it a very frequently used tool in Data Science. The Panadas library provides features using which we can read the Excel file in full as well as in parts for only a selected group of Data. We can also read an Excel file with multiple sheets in it. We use the read_excel function to read the data from it. Or download input.xlsx file here
You can create this file using the Excel Program in windows OS. Save the file as input.xlsx

id   name  zipcode
 1   ramu   522647
 2   rama   522648
 3   raji   522649
 4   rani   522650
 5  rahul   522651
 6   ravi   522652
 7   balu   522653
 8   gopi   522654
 9  gopal   522655
 10   hari   522656

Reading an Excel File

The read_excel function of the pandas library is used read the content of an Excel file into the python environment as a pandas DataFrame. The function can read the files from the OS by using proper path to the file. By default, the function will read Sheet1.

import pandas as pd
data = pd.read_excel('input.xlsx')
print(data)
Output:-

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.

Find Us On Facebook

Computer Basics

More

C Programming

More

Java Tutorial

More

Data Structures

More

MS Office

More

Database Management

More
Top