[英]Get values of columns in df according to an index list
i have a list of indexes: [0,0,0,3,1,0,4,2,1] and a dataframe (this is just an example, the real dataframe is bigger...):我有一个索引列表:[0,0,0,3,1,0,4,2,1] 和一个 dataframe(这只是一个例子,真正的 dataframe 更大......):
snr freq snr ... snr freq freq_ref
0 111.796861 400.003168 116.805099 ... 123.952201 400.046262 400.00
1 111.800587 400.010109 117.194605 ... 124.033467 400.083761 400.05
2 111.636656 400.012101 117.654265 ... 124.155229 400.117228 400.10
3 111.839271 400.031985 118.009703 ... 124.208280 400.192227 400.15
4 112.162853 400.096895 118.196040 ... 124.055698 400.218755 400.20
i want to get the snr values according to the index list.我想根据索引列表获取信噪比值。 each index is 1 snr values from a column: first index is the first snr column, 2nd index is the second snr column and the 3rd is the 3rd column.
每个索引是来自一列的 1 个 snr 值:第一个索引是第一个 snr 列,第二个索引是第二个 snr 列,第三个是第三列。 the 4th index is again the 1st snr column.
第 4 个索引再次是第一个 snr 列。
the output should be: output 应该是:
snr snr snr
0 111.796861 116.805099 123.952201
1 111.839271 117.194605 123.952201
2 112.162853 117.654265 124.033467
any ideas?有任何想法吗?
thanks谢谢
You can reshape list to 2d array first and then indexing in numpy - necessary 3 columns after selecting df['snr']
and number values in list is necessary len(a) == 3 * len(df.index)
:您可以先将列表重塑为二维数组,然后在 numpy 中进行索引 - 选择
df['snr']
后需要 3 列,并且列表中的数值是必需len(a) == 3 * len(df.index)
:
L = [0,0,0,3,1,0,4,2,1]
a = np.array(L).reshape(-1,3)
df = pd.DataFrame(df['snr'].to_numpy()[a, np.arange(a.shape[1])])
print (df)
0 1 2
0 111.796861 116.805099 123.952201
1 111.839271 117.194605 123.952201
2 112.162853 117.654265 124.033467
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.