[英]How to convert two columns of dataframe into an orderedDict in Python?
我有一個名為tableTest
的表,如下所示:
開始日期 | 結束日期 |
---|---|
2022-12-15 | 2022-12-18 |
2022-12-19 | 2022-12-21 |
2022-12-22 | 2022-12-24 |
2022-12-26 | 2022-12-27 |
2022-12-29 | 2022-12-30 |
2022-12-02 | 2022-12-04 |
2022-12-06 | 2022-12-07 |
2022-12-07 | 2022-12-08 |
2022-12-09 | 2022-12-09 |
2022-12-13 | 2022-12-14 |
我需要按原始順序循環由 startDate 和 endDate 組成的鍵值對。
我做了什么:
import pandas as pd
data = [
("2022-12-15", "2022-12-18"),
("2022-12-19", "2022-12-21"),
("2022-12-22", "2022-12-24"),
("2022-12-26", "2022-12-27"),
("2022-12-29", "2022-12-30"),
("2022-12-02", "2022-12-04"),
("2022-12-06", "2022-12-07"),
("2022-12-07", "2022-12-08"),
("2022-12-13", "2022-12-14"),
("2023-01-01", "2023-01-03"),
]
df = spark.createDataFrame(data).toDF(*('startDate', 'endDate')).toPandas()
dictTest = df.set_index('startDate')['endDate'].to_dict()
print(dictTest)
for k,v in dictTest.items():
print(f'startDate is {k} and corresponding endDate is {v}.')
上面的代碼確實可以把這兩列轉成dict,但是dict是無序的,所以我把這兩列原來的順序弄丟了。
先感謝您。
您可以使用.to_dict
的into
參數傳入OrderedDict
:
from collections import OrderedDict
dictTest = df.set_index('startDate')['endDate'].to_dict(into=OrderedDict)
只要 tableTest 是 dataframe,您就可以使用 iterrows 按原始順序迭代。
for index, row in tableTest.iterrows():
startDate = row['startDate']
endDate = row['endDate']
print(f'startDate is {startDate} and corresponding endDate is {endDate}.')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.