执行以下操作后,我有以下数据框:train_X = icon[['property', 'room', 'date', 'month', 'amount']]train_frame = train_X.groupby(['property', 'month', 'date', 'room']).median()print(train_frame) amountproperty month date room 1 6 6 2 3195.000 12 3 2977.000 18 2 3195.000 24 3 3581.000 36 2 3146.000 3 3321.500 42 2 3096.000 3 3580.000 54 2 3195.000 3 3580.000 60 2 3000.000 66 3 3810.000 78 2 3000.000 84 2 3461.320 3 2872.800 90 2 3461.320 3 3580.000 96 2 3534.000 3 2872.800 102 3 3581.000 108 3 3580.000 114 2 3195.000我的目标是根据我这样做的(房产、月份、日期、房间)跟踪中位数金额:big_list = [[property, month, date, room], ...]test_list = [property, month, date, room]if test_list == big_list: #I want to get the median amount wrt to that row which matches the test_list我该怎么做呢?我所做的是,尝试了以下...count = 0test_list = [2, 6, 36, 2]for j in big_list: if test_list == j: break count += 1现在,在获得计数后,如何通过数据帧中的计数访问中位数?他们是一种按索引访问数据帧的方法吗?请注意:big_list 是列表的列表,其中每个列表都是来自上述数据框的 [property, month, date, room]test_list 是与 big_list 匹配的传入列表,以防万一。
2 回答
收到一只叮咚
TA贡献1821条经验 获得超5个赞
回答最后一个问题: 他们是按索引访问数据帧的方法吗?
当然有 - 你应该使用 df.iloc 还是 loc 取决于你是否想通过整数获得纯粹的(我猜是这种情况) - 你应该使用 'iloc' 或例如字符串类型索引 - 然后你可以使用 loc .
文档: https ://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.iloc.html
编辑:回到问题。那么,我假设“金额”是您搜索的中位数。您可以在分组数据帧上使用 reset_index() 方法,例如
train_frame_reset = train_frame.reset_index()
然后您可以再次访问您的列名,因此您应该执行以下操作(假设 j 是找到的行的索引):
train_frame_reset.iloc[j]['amount'] <- will give you median
阿晨1998
TA贡献2037条经验 获得超6个赞
如果我正确理解您的问题,您根本不需要计算,您可以直接通过 loc 访问这些值。
看着:
A=pd.DataFrame([[5,6,9],[5,7,10],[6,3,11],[6,5,12]],columns=(['lev0','lev1','val']))
然后你做了:
test=A.groupby(['lev0','lev1']).median()
例如,访问组 lev0=6 和 lev1 =1 的中位数可以通过以下方式完成:
test.loc[6,5]
添加回答
举报
0/150
提交
取消
