我有一个关于切片数据帧的问题。我有两个数据帧:索引为3447,4024的halo_field ...
H_masa N_subs ... H_z rh
3447 1.066437e+11 1 ... 88419.632812 160354.430049
4024 4.423280e+11 1 ... 49013.289062 65239.433084
4958 3.171903e+11 1 ... 23239.701172 48248.401956
5749 2.817211e+11 1 ... 46585.765625 65032.216212
6512 2.471275e+11 1 ... 93403.398438 123058.838527我有一个名为'halo_index‘的数据帧子光环,它索引到halo_field是切片的数据帧光环中(因此我们有这样的halo_field索引)-这是subhalo.halo_index的打印输出(在右边):
0 0
1 0
2 0
3 0
4 0
...
4366516 7713551
4366517 7713552
4366518 7713553我想将子光环数据帧切片到数据帧subhalo_field中,以便它只包含具有halo_index列值的行,该列的值也包含在halo_field.index中。问题是,这两列的长度当然不一样,我不能这样做(比较行与行,将一列的所有值与另一列的所有值进行比较):
subhalo_field=subhalo[subhalo.halo_index==halo_field.index].copy()我得到了这个错误:
File "group_sh.py", line 139, in <module>
subhalo_field=subhalo[subhalo.halo_index==halo_field.index].copy()
File "/usr/local/lib/python2.7/dist-packages/pandas/core/ops.py", line 1223, in wrapper
raise ValueError('Lengths must match to compare')
ValueError: Lengths must match to compare如何对我的子光环数据帧进行切片,以便将subhalo.halo_index与halo_field.index进行比较,并将那些子光环复制到正在处理halo_index和halo_field.index的subhalo_fields中?
发布于 2018-10-29 07:39:06
如果我理解正确的话,halo_field的索引上的merge和subhalo的halo_index列可能就是您正在寻找的(这默认为内部连接行为):
halo_field.merge(subhalo, left_index=True, right_index=False, right_on='halo_index')发布于 2018-10-29 19:38:27
我找到了解决方案:
subhalo_field=subhalo[subhalo.halo_index.isin(halo_field.index)].copy()https://stackoverflow.com/questions/53036936
复制相似问题