Q:

How to read text files with Python Pandas?

belongs to collection: Python Pandas Programs

0

Pandas is a special tool which allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structure in pandas. DataFrames consists of rows, columns and the data. DataFrame can be created with the help of python dictionaries or lists but in real world, csv files are imported and then converted into DataFrames.

Pandas also support text files, and reading a text file is as similar as reading a csv file. Pandas.read_csv() is used to read the text file and the actual path of the file with .txt format will be passed inside the method. Also encoding='utf-8' will be passed to prevent from the unicodeescape error.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Let us understand with the help of an example,

# Import pandas package
import pandas as pd

# Reading a text file
data = pd.read_csv('D:/wrestlers.txt', encoding='utf-8')

# Creating a DataFrame
df = pd.DataFrame(data)

# Display DataFrame
print("Created DataFrame:\n",data)

Output:

Example: Read text files with Pandas

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python Pandas Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
What is the difference between join and merge in P... >>
<< Label encoding across multiple columns in scikit-l...