Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 How to form a dataframe reading separate dictionaries from .txt file?
#1
Hey! Got an issue here. I need to read each line of .txt file as separate dictionary. Then to organize data in columns by the key of each dictionary.
my .txt file looks as follows:
{'Name': 'John', 'Age': 27, 'Score': 23}
{'Name': 'Peter', 'Age': 29, 'Score': 25}
I need to print it out as:
Name   Age   Score
John    27     23
Peter    29     25

so far, got to this peace of code:
with open('test.txt') as f:
    data = f.readlines()
    data = [x.strip() for x in data]
import pandas as pd
data = pd.DataFrame(data)
print(data)

which would only print:
                                           0
0   {'Name': 'John', 'Age': 27, 'Score': 23}
1  {'Name': 'Peter', 'Age': 29, 'Score': 25}

Thank you and have a great day!
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  reading tab file Mandiph 1 217 Sep-05-2019, 01:03 PM
Last Post: ThomasL
  How to add a dataframe to an existing excel file wendysling 2 4,599 May-09-2019, 07:00 PM
Last Post: wendysling
  Reading a .dat file in Python mohd_umair 4 6,537 Apr-24-2019, 12:07 PM
Last Post: mohd_umair
  Is there any way to properly load fixed width file into a dataframe using Pandas? vicky53 1 521 Mar-29-2019, 06:04 PM
Last Post: Larz60+
  convert images into pixel dataframe into csv file using python synthex 3 6,005 Feb-17-2019, 06:26 AM
Last Post: scidam
  Write specific rows from pandas dataframe to csv file pradeepkumarbe 3 930 Oct-18-2018, 09:33 PM
Last Post: volcano63
  Reading time steps from nc file ankurk017 1 691 Jul-16-2018, 07:06 PM
Last Post: woooee
  reading, modifying and writing json file metalray 2 5,814 Jun-06-2018, 03:09 PM
Last Post: metalray
  Reading json file as pandas data frame? Alberto 1 5,215 Feb-05-2018, 12:43 AM
Last Post: snippsat
  matplotlib help with reading values from CSV file cps13 6 2,893 Aug-22-2017, 10:21 AM
Last Post: andre

Forum Jump:


Users browsing this thread: 1 Guest(s)