根据值或列表的变化对python数据框进行切片
Posted
技术标签:
【中文标题】根据值或列表的变化对python数据框进行切片【英文标题】:Slice python data frame based on change in value or list 【发布时间】:2018-09-16 17:56:22 【问题描述】:我有一个数据框,我想根据列值的变化将其分割成多个数据框。数据框如下所示:
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
从正面变为负面或反之亦然时,切片都需要发生。问题是我有多个数据帧要切片,每个数据帧与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
列的值变化以适用于多个数据帧的方式对数据帧进行切片?
顺便说一句:切片的结果可以是dict
、list
或dataframe
。
【问题讨论】:
我不确定我是否正确理解了这个问题,您的问题是在您拥有索引(即[16, 53, 84]
)以便在循环中使用它之后出现的? df1
、df2
... 可以是列表的元素吗?
@xg.plt.py 我可以很容易地自动收集索引值和切片数。问题是如何以自适应方式应用它们,以便它适用于具有不同切片数量和不同索引值的多个数据帧。是的,df1
、df2
可以是列表中的元素。
【参考方案1】:
您可以获得一个列表,其中每个元素都是一个数据框,与您已有的 index
列表循环。
如果len(index)==3
,考虑到index
的构建方式意味着将生成3 个数据帧,因此您实际上需要4 个分隔符。您可以在index
的开头使用None
获取它们(因为最后一行已经在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]
)。
【讨论】:
@xg.ply.py 似乎奏效了!我在玩for i in index[:-1]:
的想法,但就像你说的,范围的开始和结束是 for 循环中的技巧
应该是df_list = [image_list.iloc[index[i]:index[i+1]] for i in range(len(index)-2)]
而不是df_list = [image_list.iloc[index[i]:index[i+1]] for i in range(len(index)-1)]
,在最后停止创建一个空的数据框?
在 84 处有一个符号变化或者 ii 是数组的末尾?如果它是阵列的末端,它会系统地发生吗?现在切片边是[(None, 16), (16, 53), (53, 84), (84, None)]
,因此,如果最后一个为空,则一定意味着84是最后一行
84 是数据帧的结尾
index
是否总是包含数据帧的结尾?如果是这样,最后的None
是不必要的。【参考方案2】:
您可以创建一个唯一分配给每个符号更改事件的列。
一些样本数据
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
【讨论】:
以上是关于根据值或列表的变化对python数据框进行切片的主要内容,如果未能解决你的问题,请参考以下文章
使用索引值列表对 pandas 多索引数据框进行切片 [重复]