gpt4 book ai didi

python - 从 Dataframe 中新列的索引中提取数据

转载 作者:太空宇宙 更新时间:2023-11-03 13:55:41 30 4
gpt4 key购买 nike

如何根据不同列的索引值提取数据?

到目前为止,我能够根据同一列(5 block )中的索引号提取数据。

Dataframe 如下所示:

3017     39517.3886
3018 39517.4211
3019 39517.4683
3020 39517.5005
3021 39517.5486
5652 39628.1622
5653 39628.2104
5654 39628.2424
5655 39628.2897
5656 39628.3229
5677 39629.2020
5678 39629.2342
5679 39629.2825
5680 39629.3304
5681 39629.3628

col 中提取的数据在索引值周围 +/- 2 行

我想要一些看起来更像这样的东西:

  3017-3021   5652-5656   5677-5681
1 39517.3886 39628.1622 39629.2020
2 39517.4211 39628.2104 39629.2342
3 39517.4683 39628.2424 39629.2825
4 39517.5005 39628.2897 39629.3304
5 39517.5486 39628.3229 39629.3628

依此类推,具体取决于我要提取的数据数量。

我用来根据索引提取数据的代码是:

## find index based on the first 0 of a 000 - 111 list
a = stim_epoc[1:]
ss = [(num+1) for num,i in enumerate(zip(stim_epoc,a)) if i == (0,1)]

## extract data from a df (GCamp_ps) based on the previous index 'ss'
fin = [i for x in ss for i in range(x-2, x + 2 + 1) if i in range(len(GCaMP_ps))]
df = time_fip.loc[np.unique(fin)]
print(df)

最佳答案

形成 5 行连续的组(因为你从中心拉出 +/-2 行)。然后创建列和索引标签以及 pivot

df = df.reset_index()
s = df.index//5 # If always 5 consecutive values. I.e. +/-2 rows from a center.

df['col'] = df.groupby(s)['index'].transform(lambda x: '-'.join(map(str, x.agg(['min', 'max']))))
df['idx'] = df.groupby(s).cumcount()

df.pivot(index='idx', columns='col', values=0) # Assuming column named `0`

输出:

col   3017-3021   5652-5656   5677-5681
idx
0 39517.3886 39628.1622 39629.2020
1 39517.4211 39628.2104 39629.2342
2 39517.4683 39628.2424 39629.2825
3 39517.5005 39628.2897 39629.3304
4 39517.5486 39628.3229 39629.3628

关于python - 从 Dataframe 中新列的索引中提取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56131833/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com