我有4个列表,我想转换为熊猫数据框
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]
和我想要的输出是这样的:
carrierId DestinationID DeparturDate Prices
1 2 2 1 755
2 5 5 2 800
3 4 4 3 500
4 4 4 4 400
... ... ... ... ....
答案 0 :(得分:3)
使用移调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 |
+---+-------------+---------------+---------------+--------+
答案 1 :(得分:1)
使用字典的另一种方法
>>> import pandas as pd
>>> pd.DataFrame.from_dict({
'carrierId' : carriersID,
'DestinationID': destinationId,
'DeparturDate' : departureDate,
'Prices' : prices
}, orient='index').transpose()
答案 2 :(得分:0)
将列表转换为字典。然后将字典传递到pandas.DataFrame
,这将获得键作为列名和值行(您甚至不需要使用pandas.from_dict
)。请注意,由于列表的长度不相等,因此在创建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)
输出
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