在使用多个数据帧时,我在使用groupby
和aggregation
时遇到问题。我试图从两个不同的数据帧计算num_maint_over_$90
。在
cars_dict = {"ABC123": ["Ford", "Compact_Car"], "XYZ148": ["Chevy", "Truck"], "ASX133": ["Ford", "Truck"], "ADS111": ["Porsche", "Sports_Car"], "SSC119": ["Toyota", "Compact_Car"]}
cars = pd.DataFrame.from_dict(cars_dict, orient = 'index')
cars.columns = ["Manufacturer", "Type"]
cars.index.rename("License_Plate", inplace = True)
maintenance_dict = {"License_Plate": ["ABC123", "ABC123", "ABC123", "XYZ148", "ASX133", "ASX133", "ADS111", "ADS111", "SSC119"], "Cost": [60, 100, 200, 150, 40, 199, 33, 99, 0]}
maintenance_records = pd.DataFrame.from_dict(maintenance_dict)
maintenance_records.index.rename("order_num", inplace = True)
*汽车:
^{pr2}$*维护记录:
Cost License_Plate
order_num
0 60 ABC123
1 100 ABC123
2 200 ABC123
3 150 XYZ148
4 40 ASX133
5 199 ASX133
6 33 ADS111
7 99 ADS111
8 0 SSC119
*所需数据框:
Type num_maint_over_$90
Compact_Car 2
Sports_Car 1
Truck 2
我试过使用groupby
、apply()
、和{
下面是简单的for循环解决方案:
*df:
^{pr2}$相关问题 更多 >
编程相关推荐