1

I have been provided with data that contains multiple strings. I am to break it and split the data into three parts which are name, cost and items. I was thinking about creating empty lists and then appending the names/costs/items to after it's been sorted out. But as I try to append them it just gives me a loop of the split list.

sample_data = [
    'Laaibah,208.10,10',
    'Arnold,380.999,9',
    'Sioned,327.01,1',
    'Shayaan,429.50,2',
    'Renee,535.29,4'
]

Outcome:

    Name                Cost  Items
    Laaibah             208.10   10
    Arnold              381.00    9
    Sioned              327.01    1
    Shayaan             429.50    2
    Renee               535.29    4

The following was provided for the formatting.

Name : Left justified in 20 places
Cost : Right justified in 6 place, 2 decimal places
Items: Right justified in 5 places

Dishin H Goyani
  • 7,195
  • 3
  • 26
  • 37

1 Answers1

0

Here's what you can do:

impost pandas as pd
sample_data = [
    'Laaibah,208.10,10',
    'Arnold,380.999,9',
    'Sioned,327.01,1',
    'Shayaan,429.50,2',
    'Renee,535.29,4'
]
final_data = dict()
final_data['Name'] = list()
final_data['Cost'] = list()
final_data['Item'] = list()
for data in sample_data:
    split_data = data.split(",")
    final_data['Name'].append(split_data[0])
    final_data['Cost'].append(float(split_data[1]))
    final_data['Item'].append(int(split_data[2]))
output = pd.DataFrame(data=final_data)
Vishakha Lall
  • 1,178
  • 12
  • 33