Python Forum
Presenting multiline data into single line
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Presenting multiline data into single line
#1
Hi,

I need some help with formatting the data within my excel files.

I have an excel report that includes multiple lines of data for the same unique id which I wish to transform it into a more readable form.

For example:

Unique ID Timestamp Method Type Transaction Details [-] [+] Gross Amount
6606521 2021-06-01 10:42:00 Online Deposits Deposit 50 50
6606521 2021-06-01 10:42:00 Online Deposits Variable Commission 1.6
6606521 2021-06-01 10:42:00 Online Deposits Fixed Commission 0.29

Instead of the above, I wish to create a dataframe with 3 new columns (Variable Commission, Fixed Commission, Other Commission) that would list all information in one line.

Unique ID Timestamp Method Type Transaction Details Gross Amount Variable Commission Fixed Commission Other Commission Net Amount
6606521 2021-06-01 10:42:00 Online Deposits Deposit 50 1.6 0.29 0 48.11

How can I do this? Should I use the groupby function? The attached image might help in explaining what I am looking for better.

   

Thanks for your help! I am new to this forum and I am looking forward to learning a lot from here.

Aaron
Reply
#2
Hello mate,

If you make a second list/array to save the second table to, you can then add the data from the array/list to the main dataframe.

# Specifying each value in the new column:
df['newColumn'] = [1, 2, 3, 4]
 
# Setting each row in the new column to the same value:
df['newColumn'] = 1
 
# Creating a new column by doing a 
# calculation on an existing column:
df['newColumn'] = df['oldColumn'] * 5
You should save the spreadsheet as a CSV if possible and look at the:
import csv
Documentation here:
https://docs.python.org/3/library/csv.html

Just to give you a starting point, lets say you want to save a list of emails from a CSV to your program:
import csv

#make an empty array
list_of_emails = []

#open or read the csv
with open('users.csv',newline="") as users:
       user_read = csv.DictReader(users)
#the next bit tells it to read through each line and update the array. 
       for row in user_read:
              list_of_emails.append(row['Email'])
I hope this helps. If you have any questions then let me know on here or pm me.

Kind regards,
James
while dad_has_cigs == True:
    happiness = True
    if dad_has_cigs == False:
    print("Dad come home!")
    happiness = not happiness
    break
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  capturing multiline output for number of parameters jss 3 809 Sep-01-2023, 05:42 PM
Last Post: jss
  Need help on how to include single quotes on data of variable string hani_hms 5 2,014 Jan-10-2023, 11:26 AM
Last Post: codinglearner
  Presenting random test questions oradba4u 2 897 Sep-27-2022, 04:23 PM
Last Post: deanhystad
  beginner text formatting single line to column jafrost 4 3,207 Apr-28-2021, 07:03 PM
Last Post: jafrost
  Fetching data from multiple tables in a single request. swaroop 0 1,886 Jan-09-2021, 04:23 PM
Last Post: swaroop
  Print characters in a single line rather than one at a time hhydration 1 2,025 Oct-10-2020, 10:00 PM
Last Post: bowlofred
  Using lambdas and map() to parse substrings in a single line Drone4four 5 3,052 Sep-20-2020, 10:38 AM
Last Post: snippsat
  Support required to data log in SQL server single table via raspbeery pi rithikvg 8 4,402 Jun-23-2020, 05:23 PM
Last Post: buran
  Multiline comments macfanpl 6 2,736 May-07-2020, 08:14 PM
Last Post: macfanpl
  Python convert multi line into single line formatted string karthidec 2 9,413 Dec-23-2019, 12:46 PM
Last Post: karthidec

Forum Jump:

User Panel Messages

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