简体   繁体   English

在特定点切片 DataFrame 和 plot 每个切片

[英]Slice DataFrame at specific points and plot each slice

I am new to programming and Pythone could you help me?我是编程新手,Pythone 你能帮我吗? I have a data frame which look like this.我有一个看起来像这样的数据框。

d = {'time': [4, 10, 15, 6, 0, 20, 40, 11, 9, 12, 11, 25], 
     'value': [0, 0, 0, 50, 100, 0, 0, 70, 100, 0,100, 20]}    
df = pd.DataFrame(data=d)

I want to slice the data whenever value == 100 and then plot all slices in a figer.我想在value == 100时对数据进行切片,然后 plot 将所有切片放在一个数字中。 So my questions are how to slice or cut the data as described?所以我的问题是如何按照描述对数据进行切片或切割? and what's the best structure to save slices in order to plot?.为了 plot 保存切片的最佳结构是什么?

Note 1: value column has no frequency that I can use and it varies from 0 to 100 where time is arbitrary.注意 1:值列没有我可以使用的频率,它从 0 到 100 不等,其中时间是任意的。

Note 2: I already tried this solution but I get the same table注2:我已经尝试过这个解决方案,但我得到了同一张桌子

decreased_value = df[df['value'] <= 100][['time', 'value']].reset_index(drop=True)

How can I slice one column in a dataframe to several series based on a condition 如何根据条件将 dataframe 中的一列切成多个系列

Thanks in advance!提前致谢!

EDIT:编辑:

Here's a simpler way of handling my first answer (thanks to @aneroid for the suggestion).这是处理我的第一个答案的一种更简单的方法(感谢@aneroid 的建议)。

Get the indices where value==100 and add +1 so that these land at the bottom of each slice:获取value==100的索引并添加+1以使它们位于每个切片的底部:

indices = df.index[df['value'] == 100] + 1

Then use numpy.split (thanks to this answer for that method) to make a list of dataframes:然后使用numpy.split (感谢该方法的这个答案)制作数据帧列表:

df_list = np.split(df, indices)

Then do your plotting for each slice in a for loop:然后在 for 循环中为每个切片进行绘图:

for df in df_list:
     --- plot based on df here ---

VERBOSE / FROM SCRATCH METHOD:详细/从头开始方法:

You can get the indices for where value==100 like this:您可以像这样获取value==100的索引:

indices = df.index[df.value==100]

Then add the smallest and largest indices in order to not leave out the beginning and end of the df:然后添加最小和最大索引,以免遗漏 df 的开头和结尾:

indices = indices.insert(0,0).to_list()
indices.append(df.index[-1]+1)

Then cycle through a while loop to cut up the dataframe and put each slice into a list of dataframes:然后循环通过一个while循环来切割dataframe并将每个切片放入数据帧列表中:

i = 0
df_list = []
while i+1 < len(indices):
    df_list.append(df.iloc[indices[i]:indices[i+1]])
    i += 1

I already solved the problem using for loop , which can be used to slice and plot at the same time without using np.split function, as well as maintain the data structure.我已经使用for loop解决了这个问题,它可以在不使用np.split function 的情况下同时用于切片和 plot,以及维护数据结构。 Thanks to the previous answer by @k_n_c, it helps me improve it.感谢@k_n_c 之前的回答,它帮助我改进了它。

slices = df.index[df['score'] == 100]
slices = slices + 1

slices = np.insert(slices, 0,0, axis=0)
slices = np.append(slices,df.index[-1]+1)

prev_ind = 0
for ind in slices:
    temp = df.iloc[prev_ind:ind,:] 
    plt.plot(temp.time, temp.score)
    prev_ind = ind
plt.show() 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM