在Python中替换零子序列中的短非零子序列

时间:2017-10-19 10:06:29

标签: python python-3.x dataframe syntax

我想覆盖DataFrame列中的一些值。我的列包含一个位掩码,但我应该只留下很长的连续序列。例如,我有00010011100,结果应为00000011100。

我试过

df_norm['map'] = 0 if ((df_norm['map']==1) & (df_norm['map'].shift(periods=-1)==0) & (df_norm['map'].shift()==0)) else df_norm['map']

出了错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-100-087984ed1e2a> in <module>()
----> 1 df_norm['map'] = 0 if ((df_norm['map']==1) & (df_norm['map'].shift(periods=-1)==0) & (df_norm['map'].shift()==0)) else df_norm['map']
      2 df_norm['difference'] = df_norm['map'] - df_norm['map'].shift()

/anaconda/envs/py35/lib/python3.5/site-packages/pandas/core/generic.py in __nonzero__(self)
    915         raise ValueError("The truth value of a {0} is ambiguous. "
    916                          "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
--> 917                          .format(self.__class__.__name__))
    918 
    919     __bool__ = __nonzero__

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

这种方式适用于零子序列中的一个非零值。改变非零子序列的长度会很好。例如,lenght = 3,列为0010011110111,结果为0000011110000.怎么做?

1 个答案:

答案 0 :(得分:0)

使用apply()

df_norm['map'] = df_norm['map'].apply(lambda x: 0 if ((x==1) & (x.shift(periods=-1)==0) & (x.shift()==0)) else x)