I have these two lists:
l1 = ['ITEM #', 'UNIT PRICE']
l2 = [['123123', '$10.00'], ['456456', '$250.00'], ['789789', '$6.00']]
I want to create a dataframe like:
ITEM # | UNIT PRICE |
---|---|
123123 | $10.00 |
456456 | $250.00 |
789789 | $6.00 |
Please help with some logical explanation and code! thanks.
CodePudding user response:
Pass both lists to DataFrame
constructor, for parameters data
and columns
:
L1 = ['ITEM #', 'UNIT PRICE']
L2 = [['123123', '$10.00'], ['456456', '$250.00'], ['789789', '$6.00']]
df = pd.DataFrame(data=L2, columns=L1)
print (df)
ITEM # UNIT PRICE
0 123123 $10.00
1 456456 $250.00
2 789789 $6.00
CodePudding user response:
Simply use the DataFrame
constructor, passing the second list l2
as data (first parameter by default) and the first list l1
as column names:
l1 = ['ITEM #', 'UNIT PRICE']
l2 = [['123123', '$10.00'], ['456456', '$250.00'], ['789789', '$6.00']]
df = pd.DataFrame(l2, columns=l1)
print(df)
output:
ITEM # UNIT PRICE
0 123123 $10.00
1 456456 $250.00
2 789789 $6.00
Parameters of the DataFrame constructor:
pandas.DataFrame(data=None, index=None, columns=None, dtype=None, copy=None)