0

i have 4 list and i want to convert to panda data frame

carriersID=[1,2,3,5,6,7,8,9]
destinationId=[2,5,4,4,5,7,8,7]
departureDate=[1,2,3,4,5,7,8]
prices=[755,800,500,400,152,444,784,954,120]

and the output i want to be like this:

      carrierId     DestinationID    DeparturDate     Prices
1      2                 2               1              755
2      5                 5               2              800 
3      4                 4               3              500
4      4                 4               4              400
...  ...                 ...             ...           ....
meW
  • 3,832
  • 7
  • 27

3 Answers3

4

Use transpose T:

df = pd.DataFrame([carriersID, destinationId, departureDate, prices]).T
df.columns = ['carriersID', 'destinationId', 'departureDate', 'prices']
df


+---+-------------+---------------+---------------+--------+
|   |  carriersID | destinationId | departureDate | prices |
+---+-------------+---------------+---------------+--------+
| 0 | 1.0         | 2.0           | 1.0           |  755.0 |
| 1 | 2.0         | 5.0           | 2.0           |  800.0 |
| 2 | 3.0         | 4.0           | 3.0           |  500.0 |
| 3 | 5.0         | 4.0           | 4.0           |  400.0 |
| 4 | 6.0         | 5.0           | 5.0           |  152.0 |
| 5 | 7.0         | 7.0           | 7.0           |  444.0 |
| 6 | 8.0         | 8.0           | 8.0           |  784.0 |
| 7 | 9.0         | 7.0           | NaN           |  954.0 |
| 8 | NaN         | NaN           | NaN           |  120.0 |
+---+-------------+---------------+---------------+--------+
meW
  • 3,832
  • 7
  • 27
1

Another approach, using dictionary

>>> import pandas as pd
>>> pd.DataFrame.from_dict({
        'carrierId'    : carriersID, 
        'DestinationID': destinationId,
        'DeparturDate' : departureDate,
        'Prices'       : prices
    }, orient='index').transpose()
keepAlive
  • 6,369
  • 5
  • 24
  • 39
0

Convert the list to a dictionary. Then pass the dictionary to pandas.DataFrame, this will get the keys as column names and values rows (you do not even need to use pandas.from_dict). Note that because the length of your lists is not equal you need to take that into account and loop through the dictionary when creating the DataFrame.

import pandas as pd

carriersID = [1, 2, 3, 5, 6, 7, 8, 9]
destinationId = [2, 5, 4, 4, 5, 7, 8, 7]
departureDate = [1, 2, 3, 4, 5, 7, 8]
prices = [755, 800, 500, 400, 152, 444, 784, 954, 120]

my_dict = {'carriersID':carriersID, 'destinationId':destinationId, 'departureDate':departureDate, 'prices':prices}
df = pd.DataFrame(dict([(k, pd.Series(v)) for k,v in my_dict.items()]))
print(df)

Output

   carriersID  departureDate  destinationId  prices
0         1.0            1.0            2.0     755
1         2.0            2.0            5.0     800
2         3.0            3.0            4.0     500
3         5.0            4.0            4.0     400
4         6.0            5.0            5.0     152
5         7.0            7.0            7.0     444
6         8.0            8.0            8.0     784
7         9.0            NaN            7.0     954
8         NaN            NaN            NaN     120
b-fg
  • 3,959
  • 2
  • 28
  • 44