Python Forum
beginner text formatting single line to column
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
beginner text formatting single line to column
#5
Thank you all very much, with your help I've made considerable progress and learned a lot. I have opened the JSON directly and moved it into a dictionary. However, I'm still doing something wrong with the conversion from the dictionary to the dataframe as it will not transpose the data.

#Build dictionary
d={"freq":[FREQ], "sw":[sw], "ref":[ref], "spec":[spec], "WholeEcho":[WE], "Time":[T], "dataReal":[RD], "dataImag":[ID]}

#import pandas
import pandas as pd

# Load into Pandas,orient change so it fill(None) for missing values
df = pd.DataFrame.from_dict(d, orient='index')

#transpose index to columns
df.T
print(df)

FREQ, sw, spec, and WholeEcho are all floats. ref is a string, and T, RD, and ID are lists. My intention is that T, RD, and ID are lists of floats, but I'm not sure if I was successful.

The output is:

0
freq 1.23456e+08
sw 10000
ref [NAN]
spec 0
WholeEcho 0
Time [[0.00, 0.01, 0.02, 0.03, 0.04, 0.05]]
dataReal [[4, 6, 8, 9, 10, 12]]
dataImag [[1, 2, 3, 5, 7, 11]]

But ideally what I'd like to see in the output is...

freq 1.23456e+08
sw 10000
ref [NAN]
spec 0
WholeEcho 0

Time dataReal dataImag
0.00 4 1
0.01 6 2
0.02 8 3
0.03 9 5
0.04 10 7
0.05 12 11

Thank you for the help and direction.
Reply


Messages In This Thread
RE: beginner text formatting single line to column - by jafrost - Apr-28-2021, 07:03 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Graphic line plot with matplotlib, text file in pytho khadija 2 1,390 Aug-15-2022, 12:00 PM
Last Post: khadija
  Two text files, want to add a column value zxcv101 8 1,935 Jun-20-2022, 03:06 PM
Last Post: deanhystad
  Reshaping a single column in to multiple column using Python sahar 7 2,069 Jun-20-2022, 12:35 PM
Last Post: deanhystad
  Skipping line in text without Restarting Loop IdMineThat 4 1,491 Apr-05-2022, 04:23 AM
Last Post: deanhystad
  Transform 3 Columns into Single Column DaveG 8 1,887 Apr-04-2022, 08:42 AM
Last Post: Pedroski55
  Find and delete above a certain line in text file cubangt 12 3,492 Mar-18-2022, 07:49 PM
Last Post: snippsat
  CSV to Text File and write a line in newline atomxkai 4 2,705 Feb-15-2022, 08:06 PM
Last Post: atomxkai
  Split single column to multiple columns SriRajesh 1 1,336 Jan-07-2022, 06:43 PM
Last Post: jefsummers
  How to do next line output from CSV column? atomxkai 2 2,092 Oct-02-2021, 01:00 AM
Last Post: Pedroski55
Lightbulb Multiple inputs on the same line (beginner) dementshuk 9 2,822 Sep-03-2021, 02:21 PM
Last Post: dementshuk

Forum Jump:

User Panel Messages

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