基于对象的值在数据框中的行下降间隔
我正在尝试将数据框中的行间隔从最大值(独家)删除到列的其余(末端)。这是我的df(dflist ['time'])的一个示例:
0 0.000000
1 0.021528
2 0.042135
3 0.062925
4 0.083498
...
88 1.796302
89 1.816918
90 1.837118
91 1.857405
92 1.878976
Name: time, Length: 93, dtype: float64
我试图使用.iloc和.drop函数与.index结合使用。 :
for nested_dict in dict_all_raw.values():
for dflist in nested_dict.values():
v_max = dflist['velocity'].max()
v_max_idx = dflist['velocity'].index[dflist['velocity'] == v_max]
dflist['time'] = dflist['time'].iloc[0:[v_max_idx]]
我还尝试了几种变体,例如将“ v_max_idx”转换为带有.list或.int的列表以更改.iloc函数内部的类型,因为这似乎是问题所在:
TypeError: cannot do positional indexing on RangeIndex with these indexers [[Int64Index([15], dtype='int64')]] of type list
我不知道为什么我无法这样做,这令人沮丧,因为这似乎是一个非常基本的操作。
因此,任何帮助都将不胜感激!
dropna()问题的编辑
##关于我尝试使用.notna()的
for nested_dict in dict_all_raw.values():
for dflist in nested_dict.values():
v_max = dflist['velocity'].max()
v_max_idx = dflist['velocity'].index[dflist['velocity'] == v_max]
dflist['velocity'] = dflist['velocity'].iloc[0:list(v_max_idx)[0]]
dflist['velocity'] = dflist['velocity'][dflist['velocity'].notna()]
dflist['time'] = dflist['time'].iloc[0:list(v_max_idx)[0]]
dflist['time'] = dflist['time'][dflist['time'].notna()]
: with with with dropna():
for nested_dict in dict_all_raw.values():
for dflist in nested_dict.values():
v_max = dflist['velocity'].max()
v_max_idx = dflist['velocity'].index[dflist['velocity'] == v_max]
dflist['velocity'] = dflist['velocity'].iloc[0:list(v_max_idx)[0]].dropna()
dflist['time'] = dflist['time'].iloc[0:list(v_max_idx)[0]].dropna()
没有错误消息,它只是没有任何作用:
19 0.385243 1.272031
20 0.405416 1.329072
21 0.425477 1.352059
22 0.445642 1.349657
23 0.465755 1.378407
24 NaN NaN
25 NaN NaN
26 NaN NaN
27 NaN NaN
28 NaN NaN
29 NaN NaN
30 NaN NaN
31 NaN NaN
32 NaN NaN
33 NaN NaN
34 NaN NaN
35 NaN NaN
36 NaN NaN
I am trying to drop intervals of rows in my Dataframes from the maximal value (exclusive) to the rest (end) of the column. Here is an example of one of the column of my df (dflist['time']):
0 0.000000
1 0.021528
2 0.042135
3 0.062925
4 0.083498
...
88 1.796302
89 1.816918
90 1.837118
91 1.857405
92 1.878976
Name: time, Length: 93, dtype: float64
I have tried to use the .iloc and the .drop function in conjunction to the .index to achieve this result but without any success so far:
for nested_dict in dict_all_raw.values():
for dflist in nested_dict.values():
v_max = dflist['velocity'].max()
v_max_idx = dflist['velocity'].index[dflist['velocity'] == v_max]
dflist['time'] = dflist['time'].iloc[0:[v_max_idx]]
I have also tried several variations, like converting 'v_max_idx' to a list with .list or a .int to change the type inside the .iloc function as it seems to be the problem:
TypeError: cannot do positional indexing on RangeIndex with these indexers [[Int64Index([15], dtype='int64')]] of type list
I don't know why I am not able to do this and it is quiet frustrating, as it seems to be a pretty basic operation..
Any help would therefore be greatly appreciated !
##EDIT REGARDING THE dropna() PROBLEM
I tried with .notna() :
for nested_dict in dict_all_raw.values():
for dflist in nested_dict.values():
v_max = dflist['velocity'].max()
v_max_idx = dflist['velocity'].index[dflist['velocity'] == v_max]
dflist['velocity'] = dflist['velocity'].iloc[0:list(v_max_idx)[0]]
dflist['velocity'] = dflist['velocity'][dflist['velocity'].notna()]
dflist['time'] = dflist['time'].iloc[0:list(v_max_idx)[0]]
dflist['time'] = dflist['time'][dflist['time'].notna()]
and try with dropna():
for nested_dict in dict_all_raw.values():
for dflist in nested_dict.values():
v_max = dflist['velocity'].max()
v_max_idx = dflist['velocity'].index[dflist['velocity'] == v_max]
dflist['velocity'] = dflist['velocity'].iloc[0:list(v_max_idx)[0]].dropna()
dflist['time'] = dflist['time'].iloc[0:list(v_max_idx)[0]].dropna()
No error messages, it just doesn't do anything:
19 0.385243 1.272031
20 0.405416 1.329072
21 0.425477 1.352059
22 0.445642 1.349657
23 0.465755 1.378407
24 NaN NaN
25 NaN NaN
26 NaN NaN
27 NaN NaN
28 NaN NaN
29 NaN NaN
30 NaN NaN
31 NaN NaN
32 NaN NaN
33 NaN NaN
34 NaN NaN
35 NaN NaN
36 NaN NaN
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
pandas.Index()
< 的返回值/a> 在您的示例中是pandas.Int64Index()
。pandas.DataFrame.iloc()
允许输入像带有int 的切片对象,例如1:7
。在您的代码中,无论
v_max_idx
是pandas.Index()
对象,还是[pandas.Index()]
是列表对象,都不会不满足iloc()
参数类型的要求。您可以使用
list(v_max_idx)
将pandas.Index()
对象转换为列表,然后使用[0]
等来访问数据,喜欢Return value of
pandas.Index()
in your example ispandas.Int64Index()
.pandas.DataFrame.iloc()
allows inputs like a slice object with ints, e.g.1:7
.In your code, no matter
v_max_idx
which apandas.Index()
object or[pandas.Index()]
which is a list object doesn't meet the requirements ofiloc()
argument type.You can use
list(v_max_idx)
to convertpandas.Index()
object to list then use[0]
etc. to access the data, like