How do you convert text data to dataframe in python?

I have .TX0 file (some sort of csv txt file) and have converted this to a .txt file via python .readlines(), open(filename, 'w') etc method. I have this new saved txt file but when i try to convert it to a dataframe it's giving me only one column. the txt file is below :

Empty DataFrame
Columns: [ '"Software Version:", 6.3.2.0646, Date:, 19/08/2015 09:26:04\n',  '"Reprocess Number:", vma2:  261519, Unnamed: 7, \n',  '"Sample Name:",  , Data Acquisition Time:, 18/08/2015 17:23:23\n',  '"Instrument Name:", natural gas (PE ASXL-TCD/FID), Channel:, B\n',  '"Rack/Vial:", 0, 0.1, Operator:, joey.walker\n',  '"Sample Amount:", 1.000000, Dilution Factor:, 1.000000\n',  '"Cycle:", 1, Result File :, \\\\vma2\\TotalChrom\11170_he_tcd001.rst \n',  '"Sequence File :", \\\\vma\C1_C2_binary.seq \n',  '"===================================================================================================================================="\n',  '""\n',  '""\n'.1,  '"condensate analysis (HP4890 Optic - FID)"\n',  '"Peak", Component, Time, Area, Height, BL\n',  '"#", Name, [min], [uV*sec], [uV], \n'.1,  '------, ------, ------.1, ------.2, ------.3, ------\n',  '1, Unnamed: 55, 0.810, 706.42, 304.38, *BB\n',  '2, CH4, 0.900, 1113518.24, 495918.41, *BB\n'.1,  '3, C2H6, 1.373, 901670.23, 295381.12, *BB\n'.2,  '"", Unnamed: 73, Unnamed: 74, ------.4, ------.5, \n'.2,  '"".1, Unnamed: 79, Unnamed: 80, 2015894.89, 791603.91, \n'.3,  '"Missing Component Report"\n',  '"Component", Expected Retention (Calibration File)\n',  '------.1, ------\n'.1,  '"All components were found"\n',  '"Report stored in ASCII file :", C:\\Shared Folders\\TotalChrom\\11170_he_tcd001.TX0 \n']]
Index: []

for easier reading:

Empty DataFrame

Columns: [ '"Software Version:", 6.3.2.0646, Date:, 19/08/2015 09:26:04\n', '"Reprocess Number:", vma2: 261519, Unnamed: 7, \n', '"Sample Name:", , Data Acquisition Time:, 18/08/2015 17:23:23\n', '"Instrument Name:", natural gas (PE ASXL-TCD/FID), Channel:, B\n', '"Rack/Vial:", 0, 0.1, Operator:, joey.walker\n', '"Sample Amount:", 1.000000, Dilution Factor:, 1.000000\n', '"Cycle:", 1, Result File :, \\vma2\TotalChrom\data\Joey\Binary_Mixtures\Std1\11170_he_tcd001.rst \n', '"Sequence File :", \\vma2\TotalChrom\sequences\Joey\C1_C2_binary.seq \n', '"===================================================================================================================================="\n', '""\n', '""\n'.1, '"condensate analysis (HP4890 Optic - FID)"\n', '"Peak", Component, Time, Area, Height, BL\n', '"#", Name, [min], [uV*sec], [uV], \n'.1, '------, ------, ------.1, ------.2, ------.3, ------\n', '1, Unnamed: 55, 0.810, 706.42, 304.38, *BB\n', '2, CH4, 0.900, 1113518.24, 495918.41, *BB\n'.1, '3, C2H6, 1.373, 901670.23, 295381.12, *BB\n'.2, '"", Unnamed: 73, Unnamed: 74, ------.4, ------.5, \n'.2, '"".1, Unnamed: 79, Unnamed: 80, 2015894.89, 791603.91, \n'.3, '"Missing Component Report"\n', '"Component", Expected Retention (Calibration File)\n', '------.1, ------\n'.1, '"All components were found"\n', '"Report stored in ASCII file :", C:\Shared Folders\TotalChrom\data\Joey\Binary_Mixtures\Std1\11170_he_tcd001.TX0 \n']] Index: []

As you can see this is comma separated. Would there be any way of transferring this text to a comma delimited dataframe?

Thanks.

J

  1. HowTo
  2. Python Pandas Howtos
  3. Load Data From Text File in Pandas

Created: March-19, 2020 | Updated: December-10, 2020

  1. read_csv() Method to Load Data From Text File
  2. read_fwf() Method to Load Width-Formated Text File to Pandas DataFrame
  3. read_table() Method to Load Text File to Pandas DataFrame

We will introduce the methods to load the data from a txt file with Pandas DataFrame. We will also go through the available options.

First, we will create a simple text file called sample.txt and add the following lines to the file:

45 apple orange banana mango
12 orange kiwi onion tomato

We need to save it to the same directory from where Python script will be running.

read_csv() Method to Load Data From Text File

read_csv() is the best way to convert the text file into Pandas DataFrame. We need to set header=None as we don’t have any header in the above-created file. We can also set keep_default_na=False inside the method if we wish to replace empty values with NaN.

Example Codes:

# python 3.x
import pandas as pd
df = pd.read_csv(
    'sample.txt', sep=" ",header=None)
print(df)

Output:

    0       1       2       3       4
0  45   apple  orange  banana   mango
1  12  orange    kiwi   onion  tomato

We set sep=" " because a single white space separates values. Similarly, we can set sep="," if we read data from a comma-separated file. Replace the white spaces inside sample.txt with , and then run the code after replacing sep=" " with sep=",".

Sample.txt

45,apple,orange,banana,mango
12,orange,kiwi,,tomato

Code:

# python 3.x
import pandas as pd
df = pd.read_csv(
    'sample.txt', sep=",",header=None)
print(df)

Output:

    0       1       2       3       4
0  45   apple  orange  banana   mango
1  12  orange    kiwi     NaN  tomato

read_fwf() Method to Load Width-Formated Text File to Pandas DataFrame

read_fwf() is very helpful to load a width-formatted text file. We can’t use sep because different values may have different delimiters. Consider the following text file:

Sample.txt

45 apple  orange banana mango
12 orange kiwi   onion  tomato

In Sample.text, delimiter is not the same for all values. So read_fwf() will do the job here.

Code:

# python 3.x
import pandas as pd
df = pd.read_fwf(
    'sample.txt',header=None)
print(df)

Output:

    0       1       2       3       4
0  45   apple  orange  banana   mango
1  12  orange    kiwi   onion  tomato

read_table() Method to Load Text File to Pandas DataFrame

read_table() is another approach to load data from text file to Pandas DataFrame.

Sample.txt:

45 apple orange banana mango
12 orange kiwi onion tomato

Code:

# python 3.x
import pandas as pd
df = pd.read_table(
    'sample.txt',header=None,sep=" ")
print(df)

Output:

    0       1       2       3       4
0  45   apple  orange  banana   mango
1  12  orange    kiwi   onion  tomato

Write for us

DelftStack articles are written by software geeks like you. If you also would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

How do you convert text data to dataframe in python?

How do you convert text to a table in Python?

Using tabula: import tabula dfs = tabula. read_pdf("myfile. pdf", pages='all') # Note that dfs is list of dataframes, the tables found in the PDF.

How do you convert a dataset to a DataFrame in Python?

You can convert the sklearn dataset to pandas dataframe by using the pd. Dataframe(data=iris. data) method.

How do I convert TXT to pandas CSV?

Steps to Convert a Text File to CSV using Python.
Step 1: Install the Pandas package. If you haven't already done so, install the Pandas package. ... .
Step 2: Capture the path where your text file is stored. ... .
Step 3: Specify the path where the new CSV file will be saved. ... .
Step 4: Convert the text file to CSV using Python..

How do you convert a value into a DataFrame?

If you want to have your counts as a dataframe you can do it using function . to_frame() after the . value_counts() . If you need to name index column and rename a column, with counts in the dataframe you can convert to dataframe in a slightly different way.