我有一个数据帧,我希望根据列值的变化切换到多个数据帧。数据框如下所示:
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
每次Sign
从正变为负或变为visaversa时,需要进行切片。问题是我有多个要切片的数据帧,每个数据帧都有Sign
列的不同结构,因此一些数据帧可能有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_list
是数据框,但是这个列表需要在某种for循环中作为索引值应用。以下工作正常,但它不适应(即仅适用于image_list
)的结构。
df1 = image_list.iloc[0:index[0]]
df2 = image_list.iloc[index[0]:index[1]]
df3 = image_list.iloc[index[1]:index[2]]
因此,我如何根据 Sign
列的值的变化以适用于多个数据帧的方式对数据帧进行切片?
BTW:切片的结果可以是dict
,list
或dataframe
。
答案 0 :(得分:1)
您可以创建一个唯一分配给sign事件中每个更改的列。
一些示例数据
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
答案 1 :(得分:0)
您可以获取一个列表,其中每个元素都是使用您已有的index
列表循环的数据框。
如果len(index)==3
,考虑到index
的构建方式意味着将生成3个数据帧,因此您实际需要4个分隔符。您可以在None
开头使用index
来获取它们(因为最后一行已经在index
中)。因此,发布的代码应修改为以下内容:
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:]
是a[i:None]
)。