0

I want to create dataframe from list and I tried this code

table[1] = ['SKU1222', 'Test Import Name - Description Goes Here\nLabor - Example labor item. Quantity is number of hours spent, \nprice is hourly rate. Quantity accepts decimal values.', '1\n1.5', '$10.00\n$100.00', '$10.00\n$150.00'] import pandas as pd df = pd.DataFrame(table[1]) df = df.transpose() df 0 1 2 3 4 0 SKU1222 Test Import Name - Description Goes Here\nLabo... 1\n1.5 $10.00\n$100.00 $10.00\n$150.00 

I need table look like this...

0 1 2 3 4 5 0 SKU1222 Test Import Name - Description Goes Here 1 $10.00 $10.00 1 Labor - Example labor item 1.5 $100.00 $150.00 
11
  • do you want to split into words? Commented Feb 1, 2021 at 9:28
  • I want to split it to create dataframe Commented Feb 1, 2021 at 9:29
  • can you depict the desired result in say a table form? Commented Feb 1, 2021 at 9:30
  • You have to somehow show (manually type in) your desired output, how the resultant table (df) should look Commented Feb 1, 2021 at 10:01
  • 1
    I don't see an straightforward out-of-the-box method to achieve what you want, since arbitrarily you want to add a new row on the second column from the first break line, the problem I see is how do you know when the next item has a missing field Commented Feb 1, 2021 at 10:33

1 Answer 1

1

Your input data is in really bad shape. For now, I can suggest you the following, though this is not very useful as it would seem to me:

import pandas as pd table = [['CCooddee DDeessccrriippttiioonn QTY Price Line Total', None, None, None, None], \ ['SKU1222', 'Test Import Name - Description Goes Here\nLabor - Example labor item. Quantity is number of hours spent, \nprice is hourly rate. Quantity accepts decimal values.', '1\n1.5', '$10.00\n$100.00', '$10.00\n$150.00'], \ ['Notes', None, None, None, None], ['An invoice note can go here. Multi-line and even multi-page notes are supported.', None, None, None, None],\ ['PPaayymmeenntt DDeettaaiillss', None, None, None, None], ['', None, None, None, None]] data = [x.split('\n') for x in table[1]] maxlen = max([len(x) for x in data]) cols = ['ID', 'Desc', 'Qty', 'Price', 'Total'] data = {k:v if len(v)==maxlen else v+['']*(maxlen-len(v)) for k,v in zip(cols, data)} df = pd.DataFrame.from_dict(data) print(df) 

The dataframe generated is as follows:

 ID Desc Qty Price Total 0 SKU1222 Test Import Name - Description Goes Here 1 $10.00 $10.00 1 Labor - Example labor item. Quantity is number... 1.5 $100.00 $150.00 2 price is hourly rate. Quantity accepts decimal... 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.