<p>如果您可以接受<code>setxx</code>列的数目不一定是连续的,那么可以使用shift来检测0和非0值之间的更改,然后<code>np.split</code>分割这些更改的数据帧索引。你知道吗</p>
<p>一旦这样做了,就可以简单地为每个序列添加一个0的新列并复制其中的原始值。但由于<code>np.split</code>,使用简单的连续索引更容易。所以代码可能是:</p>
<pre><code># use a simple consecutive index
df.reset_index(inplace=True)
# split the indices on transition between null and non null values
subs = np.split(df.index.values,
df[((df.toucha == 0)&(df.toucha.shift() != 0)
|(df.toucha != 0)&(df.toucha.shift() == 0))
].index.values)
# process those sequences
for i, a in enumerate(subs):
# ignore empty or 0 value sequences
if len(a) == 0: continue
if df.toucha[a[0]] == 0: continue
df['set'+str(i)] = 0 # initialize a new column with 0
df.loc[a, 'set'+str(i)] = df.toucha.loc[a] # and copy values
# set the index back
df.set_index('Timestamp', inplace=True)
</code></pre>
<p>使用以下示例数据</p>
<pre><code> temp toucha
Timestamp
185 83 7
191 83 7
197 83 10
2051 83 10
2057 83 0
2063 83 0
2057 83 0
3000 83 0
3006 83 7
3012 83 7
3018 83 10
6000 83 10
6006 83 0
6012 83 0
6018 83 0
8000 83 0
</code></pre>
<p>它给出:</p>
<pre><code> temp toucha set0 set2
Timestamp
185 83 7 7 0
191 83 7 7 0
197 83 10 10 0
2051 83 10 10 0
2057 83 0 0 0
2063 83 0 0 0
2057 83 0 0 0
3000 83 0 0 0
3006 83 7 0 7
3012 83 7 0 7
3018 83 10 0 10
6000 83 10 0 10
6006 83 0 0 0
6012 83 0 0 0
6018 83 0 0 0
8000 83 0 0 0
</code></pre>