Handling Different Data File In Python

Shashank Shanu

8 months ago

File Handling in Python | Insideaiml
File Handling in Python | Insideaiml
This article is a basic introduction to get some idea about how to import different data into python. So, let’s start
Python provides us with many different functions to handle various types of data files. In this article, I will try to take you through some of the functions mostly used to handle different data files such as flat files, CSV and text, etc.

Handling CSV Files

One of the most commonly used data file types is a CSV data file. CSV stands for comma-separated-values. This type of file store data into a table from where rows as observations and columns as attributes.
Let’s take an example
import pandas as pd
data = pd.read_csv("People Charm case.csv")
print(data.shape)
data.head(5) 
In the above example, we used python library pandas which have a function “read_csv()” to read a CSV file. Then we print the shape of the dataset and displayed only the top 5 rows of the dataset using the head() function.

Output:

Output | Insideaiml
Output | Insideaiml

Handling Text Files

One another most common flat file type is text files. Here we will take a file that contains details of weblog in the text data file.
data2 = pd.read_table("file4.txt")

print(data2)
In the above example, we are reading a text file using pandas “read_table()” function and printing it out.

Output:

Output | Insideaiml
Output | Insideaiml

Handling excel file

We all know how popular and important excel file is and most of us used it in our day to day life. So, it does not need any detailed introduction.
Let’s take an example and see how we can read an excel file into python
data2 = pd.read_excel(“article topics.xlsx”)

data2.head()

Output:

Output | Insideaiml
Output | Insideaiml
Here we have an excel sheet and load it into python using “read_excel” function available in panda’s library.

Conclusion

In this, we learned about how to import different types of the data file into python. Note I was using Jupiter notebook here so the output looks like this. The knowledge of predictive modelling is important but equally important is knowing how to import data files such as CSV, text, Excel files from a local machine into the Python environment.
I hope you enjoyed reading this article and finally, you came to know about Handling Different Data File In Python.
For more such blogs/courses on data science, machine learning, artificial intelligence and emerging new technologies do visit us at InsideAIML.
Thanks for reading…
Happy Learning…

Submit Review