Load data from txt with pandas

Question:

I am loading a txt file containig a mix of float and string data. I want to store them in an array where I can access each element. Now I am just doing

import pandas as pd

data = pd.read_csv('output_list.txt', header = None)
print data

This is the structure of the input file: 1 0 2000.0 70.2836942112 1347.28369421 /file_address.txt.

Now the data are imported as a unique column. How can I divide it, so to store different elements separately (so I can call data[i,j])? And how can I define a header?

Asked By: albus_c

||

Answers:

You can use:

data = pd.read_csv('output_list.txt', sep=" ", header=None)
data.columns = ["a", "b", "c", "etc."]

Add sep=" " in your code, leaving a blank space between the quotes. So pandas can detect spaces between values and sort in columns. Data columns is for naming your columns.

Answered By: pietrovismara

@Pietrovismara’s solution is correct but I’d just like to add: rather than having a separate line to add column names, it’s possible to do this from pd.read_csv.

df = pd.read_csv('output_list.txt', sep=" ", header=None, names=["a", "b", "c"])
Answered By: Sam Perry

I’d like to add to the above answers, you could directly use

df = pd.read_fwf('output_list.txt')

fwf stands for fixed width formatted lines.

you can use this

import pandas as pd
dataset=pd.read_csv("filepath.txt",delimiter="t")
Answered By: ramakrishnareddy

You can do as:

import pandas as pd
df = pd.read_csv('file_locationfilename.txt', delimiter = "t")

(like, df = pd.read_csv(‘F:Desktopdstext.txt’, delimiter = “t”)

Answered By: tulsi kumar

If you don’t have an index assigned to the data and you are not sure what the spacing is, you can use to let pandas assign an index and look for multiple spaces.

df = pd.read_csv('filename.txt', delimiter= 's+', index_col=False)
Answered By: bfree67

You can import the text file using the read_table command as so:

import pandas as pd
df=pd.read_table('output_list.txt',header=None)

Preprocessing will need to be done after loading

Answered By: Kaustubh J

Based on the latest changes in pandas, you can use, read_csv , read_table is deprecated:

import pandas as pd
pd.read_csv("file.txt", sep = "t")
Answered By: pari

I usually take a look at the data first or just try to import it and do data.head(), if you see that the columns are separated with t then you should specify sep="t" otherwise, sep = " ".

import pandas as pd     
data = pd.read_csv('data.txt', sep=" ", header=None)
Answered By: Mohamed Berrimi

You can use it which is most helpful.

df = pd.read_csv(('data.txt'), sep="t", skiprows=[0,1], names=['FromNode','ToNode'])
Answered By: Sunil Singh

If you want to load the txt file with specified column name, you can use the code below. It worked for me.

import pandas as pd    
data = pd.read_csv('file_name.txt', sep = "t", names = ['column1_name','column2_name', 'column3_name'])
Answered By: mpriya
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.