Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于值或列表中的更改对python数据帧进行切片_Python_Pandas_Dataframe_Slice - Fatal编程技术网

基于值或列表中的更改对python数据帧进行切片

基于值或列表中的更改对python数据帧进行切片,python,pandas,dataframe,slice,Python,Pandas,Dataframe,Slice,我有一个数据帧,我想根据列值的变化将其分为多个数据帧。数据帧看起来像: Image Yaw Sign 0 IMG_170705_121224_0148_GRE_vig_ortho_correct.tif -41.299461 -1.0 1 IMG_170705_121226_0149_GRE_vig_ortho_correct.tif -39.885353 -1.0 2

我有一个数据帧,我想根据列值的变化将其分为多个数据帧。数据帧看起来像:

                                               Image         Yaw  Sign
0   IMG_170705_121224_0148_GRE_vig_ortho_correct.tif  -41.299461  -1.0
1   IMG_170705_121226_0149_GRE_vig_ortho_correct.tif  -39.885353  -1.0
2   IMG_170705_121228_0150_GRE_vig_ortho_correct.tif  -38.424816  -1.0
3   IMG_170705_121230_0151_GRE_vig_ortho_correct.tif  -44.121506  -1.0
4   IMG_170705_121232_0152_GRE_vig_ortho_correct.tif  -43.348404  -1.0
5   IMG_170705_121234_0153_GRE_vig_ortho_correct.tif  -33.564381  -1.0
6   IMG_170705_121236_0154_GRE_vig_ortho_correct.tif  -22.381189  -1.0
7   IMG_170705_121238_0155_GRE_vig_ortho_correct.tif  -24.130825  -1.0
8   IMG_170705_121240_0156_GRE_vig_ortho_correct.tif  -36.879814  -1.0
9   IMG_170705_121242_0157_GRE_vig_ortho_correct.tif  -32.717499  -1.0
10  IMG_170705_121244_0158_GRE_vig_ortho_correct.tif  -55.632034  -1.0
11  IMG_170705_121246_0159_GRE_vig_ortho_correct.tif  -41.810268  -1.0
12  IMG_170705_121248_0160_GRE_vig_ortho_correct.tif   -38.68877  -1.0
13  IMG_170705_121250_0161_GRE_vig_ortho_correct.tif  -38.238991  -1.0
14  IMG_170705_121252_0162_GRE_vig_ortho_correct.tif  -33.106453  -1.0
15  IMG_170705_121254_0163_GRE_vig_ortho_correct.tif  -25.821913  -1.0
16  IMG_170705_121256_0164_GRE_vig_ortho_correct.tif   56.908508   1.0
17  IMG_170705_121258_0165_GRE_vig_ortho_correct.tif    48.51984   1.0
18  IMG_170705_121300_0166_GRE_vig_ortho_correct.tif  114.620369   1.0
19  IMG_170705_121302_0167_GRE_vig_ortho_correct.tif  106.544044   1.0
20  IMG_170705_121304_0168_GRE_vig_ortho_correct.tif  105.703751   1.0
21  IMG_170705_121306_0169_GRE_vig_ortho_correct.tif  111.010986   1.0
22  IMG_170705_121308_0170_GRE_vig_ortho_correct.tif  100.446739   1.0
23  IMG_170705_121310_0171_GRE_vig_ortho_correct.tif   87.035179   1.0
24  IMG_170705_121312_0172_GRE_vig_ortho_correct.tif   93.275948   1.0
25  IMG_170705_121314_0173_GRE_vig_ortho_correct.tif   84.998108   1.0
26  IMG_170705_121316_0174_GRE_vig_ortho_correct.tif   97.052902   1.0
27  IMG_170705_121318_0175_GRE_vig_ortho_correct.tif   99.751534   1.0
28  IMG_170705_121320_0176_GRE_vig_ortho_correct.tif   97.002548   1.0
29  IMG_170705_121322_0177_GRE_vig_ortho_correct.tif    98.25058   1.0
..                                               ...         ...   ...
54  IMG_170705_121412_0202_GRE_vig_ortho_correct.tif  -71.117188  -1.0
55  IMG_170705_121414_0203_GRE_vig_ortho_correct.tif  -55.625908  -1.0
56  IMG_170705_121416_0204_GRE_vig_ortho_correct.tif  -49.295944  -1.0
57  IMG_170705_121418_0205_GRE_vig_ortho_correct.tif  -36.872471  -1.0
58  IMG_170705_121420_0206_GRE_vig_ortho_correct.tif   -34.20092  -1.0
59  IMG_170705_121422_0207_GRE_vig_ortho_correct.tif  -34.930763  -1.0
60  IMG_170705_121424_0208_GRE_vig_ortho_correct.tif  -37.000858  -1.0
61  IMG_170705_121426_0209_GRE_vig_ortho_correct.tif  -39.504391  -1.0
62  IMG_170705_121428_0210_GRE_vig_ortho_correct.tif  -41.150524  -1.0
63  IMG_170705_121430_0211_GRE_vig_ortho_correct.tif  -39.845219  -1.0
64  IMG_170705_121432_0212_GRE_vig_ortho_correct.tif   -39.10614  -1.0
65  IMG_170705_121434_0213_GRE_vig_ortho_correct.tif  -35.891712  -1.0
66  IMG_170705_121436_0214_GRE_vig_ortho_correct.tif   -37.41824  -1.0
67  IMG_170705_121438_0215_GRE_vig_ortho_correct.tif  -34.713837  -1.0
68  IMG_170705_121440_0216_GRE_vig_ortho_correct.tif  -48.803596  -1.0
69  IMG_170705_121442_0217_GRE_vig_ortho_correct.tif  -44.784882  -1.0
70  IMG_170705_121444_0218_GRE_vig_ortho_correct.tif  -40.010029  -1.0
71  IMG_170705_121446_0219_GRE_vig_ortho_correct.tif  -42.793995  -1.0
72  IMG_170705_121448_0220_GRE_vig_ortho_correct.tif  -41.527176  -1.0
73  IMG_170705_121450_0221_GRE_vig_ortho_correct.tif  -39.461327  -1.0
74  IMG_170705_121452_0222_GRE_vig_ortho_correct.tif  -39.929741  -1.0
75  IMG_170705_121454_0223_GRE_vig_ortho_correct.tif  -40.532288  -1.0
76  IMG_170705_121456_0224_GRE_vig_ortho_correct.tif   -45.85107  -1.0
77  IMG_170705_121458_0225_GRE_vig_ortho_correct.tif  -41.356819  -1.0
78  IMG_170705_121500_0226_GRE_vig_ortho_correct.tif  -45.120956  -1.0
79  IMG_170705_121502_0227_GRE_vig_ortho_correct.tif  -49.955151  -1.0
80  IMG_170705_121504_0228_GRE_vig_ortho_correct.tif  -54.691364  -1.0
81  IMG_170705_121506_0229_GRE_vig_ortho_correct.tif  -47.738556  -1.0
82  IMG_170705_121508_0230_GRE_vig_ortho_correct.tif  -37.778706  -1.0
83  IMG_170705_121510_0231_GRE_vig_ortho_correct.tif  -39.388027  -1.0
每次
符号
从正值变为负值或visaversa时,都需要进行切片。问题是我有多个要切片的数据帧,每个数据帧与
符号
列的结构不同,因此一些数据帧可能有3个切片(就像这个一样),而其他数据帧可能有更多切片

我可以很容易地通过以下方法获得切片的索引值:

for mid, group in itertools.groupby(image_list['Sign'], key=operator.itemgetter(0)):
    length.append(len(list(group)))

index = [] # store the index values for splitting the dataframe
total = 0 # reset total value

for i in length: # loop through length values for each 'group'
    total = total +i # add each value to get compound index values
    index.append(total) # these are the index values to split the dataframe
这给了我
[16,53,84]
其中
image\u list
是数据帧,但是这个列表需要作为某种类型的for循环中的索引值应用。以下方法可以很好地工作,但它不是自适应的(即,仅适用于
图像列表
的结构)

因此,如何根据
符号
列的值的变化,以适用于多个数据帧的方式对数据帧进行切片?


顺便说一句:切片的结果可以是
dict
列表
数据帧

您可以获得一个列表,其中每个元素都是一个数据帧,与您已有的
索引
列表循环

如果
len(index)==3
,考虑到
index
的构建方式意味着将有3个数据帧要生成,因此实际上需要4个分隔符。您可以使用
索引
开头的
获取它们(因为最后一行已经在
索引
中)。因此,发布的代码应修改为以下内容:

index = [None] # store the index values for splitting the dataframe, a 0 would work too
total = 0 # reset total value

for i in length: # loop through length values for each 'group'
    total = total +i # add each value to get compound index values
    index.append(total) # these are the index values to split the dataframe
它将返回一个包含
[None,16,53,84]
的列表。使用此列表,可以在边缘进行切片而不会出现问题:

df_list = [image_list.iloc[index[i]:index[i+1]] for i in range(len(index)-1)]

这利用了
a[None:i]
相当于
a[:i]
(而且,
a[i://code>是
a[i:None]
)。

您可以创建一个列,该列被唯一地分配给符号事件中的每个更改

一些样本数据

df = pd.DataFrame({'Image':list('xxxxxxxxxxxxxxx'),'Sign':[1,1,-1,-1,1,1,-1,-1,-1,1,1,1,-1,-1,-1]})
   Image  Sign
0      x     1
1      x     1
2      x    -1
3      x    -1
4      x     1
5      x     1
6      x    -1
7      x    -1
8      x    -1
9      x     1
10     x     1
11     x     1
12     x    -1
13     x    -1
14     x    -1
现在使用
cumsum()
shift
查找符号更改的位置,并将该值赋回数据帧

df['groups'] = (df.Sign != df.Sign.shift(1)).cumsum()
现在我们可以
groupby
[groups]
,并将原始数据帧的片段存储在列表中

frames = [frame for _,frame in df.groupby('groups')]
frames[0]

  Image  Sign  groups
0     x     1       1
1     x     1       1

我不确定我是否正确理解了这个问题,你的问题是在你有索引(即
[16,53,84]
)以便在循环中使用它之后提出的?
df1
df2
。。。是否是列表的元素?@xg.plt.py我可以很容易地自动收集索引值和切片数。问题是如何以一种自适应的方式应用它们,以便它能够适用于具有不同片数和不同索引值的多个数据帧。是的,
df1
df2
可以是列表中的元素。@xg.ply.py似乎很有效!我一直在玩弄索引中I的
:-1]:
,但正如你所说,范围的开始和结束都是循环中的把戏,如果
df_list=[image_list.iloc[index[I]:index[I+1]]表示范围内I(len(index)-2)]
而不是
df_list=[image_list.iloc[index[I]:index[I+1]]表示范围内I(len(index)-1)]
要停止在末尾创建空数据帧吗?在84处有符号更改,还是在数组末尾?如果它是数组的结尾,它会系统地发生吗?现在切片边是
[(None,16),(16,53),(53,84),(84,None)]
,因此,如果最后一行是空的,则必须意味着84是最后一行84是数据帧的结尾。索引将
始终包含数据帧的结尾吗?如果是,则不需要最后的
None
frames = [frame for _,frame in df.groupby('groups')]
frames[0]

  Image  Sign  groups
0     x     1       1
1     x     1       1