我正在使用两个键将数据帧转换为字典,其中一个来自索引,另一个来自列。我找到了一种“for循环”的方法。我很好奇,有没有更有效的方法呢?
HarvesterData_df = pd.read_csv('HarvesterData.csv').set_index('Feature')
HarvesterData_df.index.name = None
数据框看起来像这样。
265 HP Combine 305 HP Combine 385 HP Combine
PurchasePrice 291550.00 342125.00 430950.00
AnnualInterest 42671.91 50074.18 63074.80
Throughput 33.19 44.26 66.39
Speed 5.50 5.50 5.50
Width 4.57 6.10 9.14
Efficiency 0.90 0.90 0.90
Horsepower 265.00 305.00 385.00
FuelConsumption 11.61 13.36 16.86
LaborCost 14.50 14.50 14.50
CombinePrice 293000.00 337000.00 407000.00
CornHeadPrice 50000.00 65500.00 100000.00
RowNumber 6.00 8.00 12.00
TheorticalThroughput 27.66 36.88 55.32
我想将它转换为字典,其中包含基于索引和列的键。我试试:
HarvesterData = {(r,c): HarvesterData_df.loc[r][c] for r in HarvesterData_df.index.tolist() for c in HarvesterData_df.columns.tolist()}
输出完美,正是我想要的。但是,我可以在Pandas中没有循环吗?
{('AnnualInterest', '265 HP Combine'): 42671.910000000003,
('AnnualInterest', '305 HP Combine'): 50074.18,
('AnnualInterest', '385 HP Combine'): 63074.800000000003,
('AnnualInterest', '440 HP Combine'): 72778.619999999995,
('CombinePrice', '265 HP Combine'): 293000.0,
('CombinePrice', '305 HP Combine'): 337000.0,
('CombinePrice', '385 HP Combine'): 407000.0,
('CombinePrice', '440 HP Combine'): 455000.0,
('CornHeadPrice', '265 HP Combine'): 50000.0,
('CornHeadPrice', '305 HP Combine'): 65500.0,
('CornHeadPrice', '385 HP Combine'): 100000.0,
('CornHeadPrice', '440 HP Combine'): 130000.0,
('Efficiency', '265 HP Combine'): 0.90000000000000002,
('Efficiency', '305 HP Combine'): 0.90000000000000002,
('Efficiency', '385 HP Combine'): 0.90000000000000002,
('Efficiency', '440 HP Combine'): 0.90000000000000002, ...