The Python Oracle

Load data from txt with pandas

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Thinking It Over

--

Chapters
00:00 Load Data From Txt With Pandas
00:41 Answer 1 Score 353
01:08 Answer 2 Score 150
01:26 Accepted Answer Score 78
01:52 Answer 4 Score 55
02:14 Answer 5 Score 34
02:25 Thank you

--

Full question
https://stackoverflow.com/questions/2154...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #io #pandas

#avk47



ANSWER 1

Score 359


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.




ANSWER 2

Score 156


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.




ANSWER 3

Score 55


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



ANSWER 4

Score 34


you can use this

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