Python Data Processing
Python Data CSV Processing
In this tutorial we will discuss reading the csv(comma separated values) it is the fundamental necessary topic we will learn about.Pandas Libraries Provides the full features of reading the csv with full control over the file .
Input as CSV file
CSV is the text file which contain the raw material of raw data ,with help of pandas we create the useful information
Name,Phone,Address,RollNo,Class Rohan,8765435687,India,001,5 Sohan,4565465466,India,002,5 Reema,4987654765,India,003,5 Archana,987654556,India,004,5
Save as this file with the name of info.csv,you can write on notepad by copy and paste the data.
Lets import in Program
Reading CSV file
read_csv function is the library of pandas with the help of this function we can read the file.
import pandas as pd #importing pandas library abc = pd.read_csv('path/info.csv') #read info.csv file print (abc) #print abc file
when we execute this program the following output .
Name Phone Address RollNo Class Rohan 8765435687 India 001 5 Sohan 4565465466 India 002 5 Reema 4987654765 India 003 5 Archana 987654556 India 004 5
Reading Specific Rows
Pandas library also used for printing the specific data, We slice the result from the read_csv function using code lets understand by the demonstration.
#import pandas library import pandas as pd abc = pd.read_csv('path/info.csv') # Slice the result for first 2 rows print (abc[0:2]['Name'])
0 Rohan 1 Sohan
Reading Specific Column
Pandas also provides the facility of read any specific column to process the data.
import pandas as pd #importing info.csv file data = pd.read_csv('path/info.csv') # Use more then one column print (data.loc[:,['Name','Address']])
When using the loc() function meaning that we use the more than dimension.
0 Rohan India 1 Sohan India 2 Reema India 3 Archana India