Pandas多级索引和约束返回不可对齐的布尔系列。

huangapple go评论56阅读模式
英文:

Pandas multiindex and constraint returns unalignable boolean series

问题

要使用pandas的多重索引并根据次要索引和单列值的约束进行排序。以下是您要求的翻译:

# 选择特定Frame_ID范围内,且Lane_ID等于特定值的数据
idx = pd.IndexSlice
nl_df = trajec.loc[idx[:, 200:300, :], 'Lane_ID'] == 2

上述代码将帮助您根据Frame_ID和Lane_ID的条件选择数据,并存储在nl_df中以进行进一步分析。

英文:

Working with pandas multiindex and want to sort by secondary index and a constraint on a single column value.

		Local_X	Local_Y	v_Length	v_Width	v_Class	v_Vel	v_Acc	Lane_ID	Preceding	Following	Space_Headway	Time_Headway	ewm_y	ewm_x	ewm_v	ewm_a
Vehicle_ID	Frame_ID																
1	200	17.070	233.130	14.3	6.4	2	14.51	0.00	2	0	11	0.00	0.00	230.228002	17.035639	14.505089	0.463977
    201	17.087	234.581	14.3	6.4	2	14.51	0.00	2	0	11	0.00	0.00	231.679002	17.052759	14.505982	0.441342
    202	17.104	236.032	14.3	6.4	2	14.51	0.00	2	0	11	0.00	0.00	233.130001	17.069839	14.505803	0.419812
    203	17.121	237.482	14.3	6.4	2	14.51	0.00	2	0	11	0.00	0.00	234.580667	17.086893	14.505657	0.399332
    204	17.138	238.933	14.3	6.4	2	14.51	0.00	2	0	11	0.00	0.00	236.031445	17.103929	14.506447	0.379851
...	...	...	...	...	...	...	...	...	...	...	...	...	...	...	...	...	...
90	296	32.932	69.219	13.4	5.3	2	8.87	-0.85	3	86	0	58.82	6.63	67.578847	32.931133	8.951475	-0.178712
    297	32.933	70.093	13.4	5.3	2	8.85	0.35	3	86	0	58.95	6.66	68.426700	32.931763	8.910204	-0.094582
    298	32.930	70.970	13.4	5.3	2	8.97	1.88	3	86	0	59.07	6.59	69.281051	32.931170	8.897905	0.152664
    299	32.930	71.863	13.4	5.3	2	9.26	3.86	3	86	0	59.18	6.39	70.146146	32.930778	8.949272	0.514508
    300	32.945	72.801	13.4	5.3	2	9.67	4.80	3	86	0	59.22	6.12	71.034139	32.935535	9.091710	0.885495

I want to be able to sort out slices of Frame_ID that belong between values and where Lane_ID == cerrtain value.

idx = pd.IndexSlice
nl_df = trajec.loc[idx[:,200:300], :]

This above works to get the multiindex frame where Frame_ID fits certain values but cannot add in the lane_id consideration.

trajec.loc[idx[:,200:300], 'Lane_ID'] == 2

returns the boolean series but I want the entire dataframe for these conditions to be able to perform further analysis. I cannot however combine the 2 like with a normal dataframe and .loc for some reason.

I can perform the action in 2 lines by combining the 2 commands above but there probably is some way to solve with 1 line. I just can't figure it out.

答案1

得分: 0

这一行代码的翻译如下:

df.loc[idx[df.loc[idx[:,:], 'Lane_ID'] == 2,200:300],:]
英文:

Don't know if I understood you correctly, maybe this line solves the problem?

df.loc[idx[df.loc[idx[:,:], 'Lane_ID'] == 2,200:300],:]

huangapple
  • 本文由 发表于 2023年3月9日 23:15:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75686563.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定