281

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?

albus_c
  • 6,292
  • 14
  • 36
  • 77

11 Answers11

348

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.

Chrisji
  • 311
  • 2
  • 13
pietrovismara
  • 6,102
  • 5
  • 33
  • 45
148

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.

Meenakshi Ravisankar
  • 1,580
  • 1
  • 8
  • 4
74

You can do as:

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

(like, df = pd.read_csv('F:\Desktop\ds\text.txt', delimiter = "\t")

Rajat Jain
  • 1,339
  • 2
  • 16
  • 29
tulsi kumar
  • 986
  • 8
  • 6
54

@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"])
Sam Perry
  • 2,554
  • 3
  • 28
  • 29
34

you can use this

import pandas as pd
dataset=pd.read_csv("filepath.txt",delimiter="\t")
Marian Nasry
  • 821
  • 9
  • 22
ramakrishnareddy
  • 611
  • 1
  • 6
  • 13
28

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)
bfree67
  • 669
  • 7
  • 6
  • 4
    Equivalently you can specify the more verbose argument `delim_whitespace=True` instead of the `'\s+'` delimiter – ALollz Aug 28 '19 at 18:55
9

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'])
mpriya
  • 823
  • 8
  • 15
8

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")
pari
  • 788
  • 8
  • 12
7

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

Kaustubh J
  • 742
  • 8
  • 9
2

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)
  • Carefully adding "header=None" and adding an additional row with the max number of columns, you will get errors like "pandas.errors.ParserError: Error tokenizing data. C error: Expected N fields in line M" very hard to understand why. Removing "header=None" fix the problem. – Gustavo Rodríguez Oct 18 '21 at 10:17
2

You can use it which is most helpful.

df = pd.read_csv(('data.txt'), sep="\t", skiprows=[0,1], names=['FromNode','ToNode'])
Sunil Singh
  • 187
  • 2
  • 12