将列表转换为熊猫数据框python [重复]
Posted
技术标签:
【中文标题】将列表转换为熊猫数据框python [重复]【英文标题】:Convert list to panda data frame python [duplicate] 【发布时间】:2019-05-25 10:14:01 【问题描述】:我有 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
... ... ... ... ....
【问题讨论】:
您尝试过什么,出了什么问题? 【参考方案1】:使用转置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 |
+---+-------------+---------------+---------------+--------+
【讨论】:
【参考方案2】:另一种方法,使用字典
>>> import pandas as pd
>>> pd.DataFrame.from_dict(
'carrierId' : carriersID,
'DestinationID': destinationId,
'DeparturDate' : departureDate,
'Prices' : prices
, orient='index').transpose()
【讨论】:
【参考方案3】:将列表转换为字典。然后将字典传递给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
【讨论】:
以上是关于将列表转换为熊猫数据框python [重复]的主要内容,如果未能解决你的问题,请参考以下文章