Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/364.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_Default Value_Identity Column - Fatal编程技术网

Python 将要选择/取消选择的列值定义为默认值

Python 将要选择/取消选择的列值定义为默认值,python,pandas,default-value,identity-column,Python,Pandas,Default Value,Identity Column,我想在一列中自动选择值-步骤ID。 除了定义要过滤的步骤ID(如下面的代码所示)之外,我还要定义要排除的第一个步骤ID和最后一个步骤ID df = df.set_index(['Step_ID']) df.loc[df.index.isin(['Step_2','Step_3','Step_4','Step_5','Step_6','Step_7','Step_8','Step_9','Step_10','Step_11','Step_12','Step_13','Step_14','Step

我想在一列中自动选择值-步骤ID。 除了定义要过滤的步骤ID(如下面的代码所示)之外,我还要定义要排除的第一个步骤ID和最后一个步骤ID

df = df.set_index(['Step_ID'])

df.loc[df.index.isin(['Step_2','Step_3','Step_4','Step_5','Step_6','Step_7','Step_8','Step_9','Step_10','Step_11','Step_12','Step_13','Step_14','Step_15','Step_16','Step_17','Step_18','Step_19','Step_20','Step_21','Step_22','Step_23','Step_24'])]
是否有排除列中第一个和最后一个值的选项?在本例中,步骤1和步骤25。 或者包括第一个和最后一个值以外的所有值?在本例中,步骤2-步骤24

原因是文件的“步骤ID”编号不同。 因为我不必一直重新定义它,所以我希望有一个解决方案来简化对它们的过滤。有必要排除“步骤ID”列中的第一个和最后一个值,但步骤ID的数量始终不同

通过步骤1-步骤X,我需要步骤2-步骤(X-1)

使用:

df = pd.DataFrame({
    'Step_ID': ['Step_1','Step_1','Step_2','Step_2','Step_3','Step_4','Step_5',
                'Step_6','Step_6'],
    'B': list(range(9))})
print (df)
  Step_ID  B
0  Step_1  0
1  Step_1  1
2  Step_2  2
3  Step_2  3
4  Step_3  4
5  Step_4  5
6  Step_5  6
7  Step_6  7
8  Step_6  8
选择没有通过切片提取的第一个和最后一个索引值的所有索引值。索引[[0,-1]]:

df = df.set_index(['Step_ID'])
df = df.loc[~df.index.isin(df.index[[0, -1]].tolist())]
print (df)
         B
Step_ID   
Step_2   2
Step_2   3
Step_3   4
Step_4   5
Step_5   6

这个问题不是很清楚,你们可能需要详细说明一下。评论不是为了扩大讨论;这段对话已经结束。