Load data from txt with pandas

PythonIoPandas

Python Problem Overview


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?

Python Solutions


Solution 1 - Python

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.

Solution 2 - Python

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.

Solution 3 - Python

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")

Solution 4 - Python

@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"])

Solution 5 - Python

you can use this

import pandas as pd
dataset=pd.read_csv("filepath.txt",delimiter="\t")

Solution 6 - Python

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)

Solution 7 - Python

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

Solution 8 - Python

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")

Solution 9 - Python

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'])

Solution 10 - Python

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)

Solution 11 - Python

You can use it which is most helpful.

df = pd.read_csv(('data.txt'), sep="\t", skiprows=[0,1], names=['FromNode','ToNode'])

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionalbus_cView Question on Stackoverflow
Solution 1 - PythonpietrovismaraView Answer on Stackoverflow
Solution 2 - PythonMeenakshi RavisankarView Answer on Stackoverflow
Solution 3 - Pythontulsi kumarView Answer on Stackoverflow
Solution 4 - PythonSam PerryView Answer on Stackoverflow
Solution 5 - PythonramakrishnareddyView Answer on Stackoverflow
Solution 6 - Pythonbfree67View Answer on Stackoverflow
Solution 7 - PythonKaustubh JView Answer on Stackoverflow
Solution 8 - PythonpariView Answer on Stackoverflow
Solution 9 - PythonmpriyaView Answer on Stackoverflow
Solution 10 - PythonMohamed BerrimiView Answer on Stackoverflow
Solution 11 - PythonSunil SinghView Answer on Stackoverflow