Python Forum
Simple question (I guess)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple question (I guess)
#1
I have a txt file with data, sctructured this way:

Output:
[ [ 1, "ABRAHAN LINFE", "JUNIAN", 10, 2011, "$ 6.277,35" ], [ 1, "ABRAHAN LINFE", "JUNIAN", 9, 2011, "$ 6.372,35" ], [ 1, "ABRAHAN LINFE", "JUNIAN", 9, 2011, "$ 6.372,35" ] ]
The file have millions of records,
How could I load this file into a dataframe in python? What is the best way to do it?
Reply
#2
This looks like JSON, so pandas.read_json() should work
spam="""[ 
[
    1,
    "ABRAHAN LINFE",
    "JUNIAN",
    10,
    2011,
    "$ 6.277,35"
  ],
[
    1,
    "ABRAHAN LINFE",
    "JUNIAN",
    9,
    2011,
    "$ 6.372,35"
  ],
[
    1,
    "ABRAHAN LINFE",
    "JUNIAN",
    9,
    2011,
    "$ 6.372,35"
  ]
]"""

import pandas as pd

df = pd.read_json(spam)
print(df)
output
Output:
0 1 2 3 4 5 0 1 ABRAHAN LINFE JUNIAN 10 2011 $ 6.277,35 1 1 ABRAHAN LINFE JUNIAN 9 2011 $ 6.372,35 2 1 ABRAHAN LINFE JUNIAN 9 2011 $ 6.372,35
Of course you can pass a file path or file object instead of string
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Simple pandas question mcva 4 2,664 Dec-17-2021, 04:47 PM
Last Post: mcva
  Simple pandas dataframe question popohoma 1 3,556 Jan-03-2019, 05:00 PM
Last Post: ashlardev
  very simple dataframe question really_this_dumb 0 2,200 Jan-02-2018, 11:30 AM
Last Post: really_this_dumb

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020