python-3.x - How can you find when a value changes throughout every row in a data frame?
问题描述
I am attempting to label accounts as new, current, lost, or returning but am having trouble with the logic. The row index is the account and the columns are the years and the values are 1's and 0's representing if the account is active or not. This is what i came up with so far. I'm not sure if this will ever work or if i'm close and I'm not sure how the logic would look for returning customers.
df2 is the original data frame and df3 = df2.shift(periods=1,axis=1)
def differences():
if df2 != df3 & df2 == 1:
return "New"
elif df2 != df3 & df2 ==0:
return "Lost"
elif df2 == df3 & df2 ==0:
return ""
else:
return "Continuing"
differences()
`
And when I run this code i get the following error:
couldn't find matching opcode for 'and_bdl'
解决方案
以下代码逻辑可能适用于您的情况。
编辑:根据您的评论,我修改了代码,以便检查除最后一列之外的所有列。
import pandas as pd
str="""account 2019 2018 2017 2016 2015
alex 1 0 0 0 0
joe 0 0 1 0 0
boss 1 1 1 1 1
smith 1 1 0 1 0"""
df = pd.read_csv(pd.io.common.StringIO(str), sep='\s+', index_col='account')
df
#Out[46]:
# 2019 2018 2017 2016 2015
#account
#alex 1 0 0 0 0
#joe 0 0 1 0 0
#boss 1 1 1 1 1
#smith 1 1 0 1 0
# find account status per-year
def account_status(x):
status = []
n = x.size
for i in range(n-1):
if x.iloc[i] == 1:
# if all rest are '0'
if x.iloc[i+1:].eq(0).all():
status.extend(['new'] + [None]*(n-i-2))
break
# if the previous year is '0'
elif x.iloc[i+1] == 0:
status.append('returning')
else:
status.append('continuing')
else:
# at least one '1' in previous years
if x.iloc[i+1:].eq(1).any():
status.append('lost')
else:
status.extend([None] * (n-i-1))
break
return status
s = df.apply(account_status, axis=1).apply(pd.Series)
s.columns = df.columns[:-1]
s
#Out[57]:
# 2019 2018 2017 2016
#account
#alex new None None None
#joe lost lost new None
#boss continuing continuing continuing continuing
#smith continuing returning lost new
推荐阅读
- mobile - 垂直字符识别
- linux - 如何将无线接入点更改为无线客户端?
- docker - 加入 Org3 网络,使用单独的排序服务和 zookeeper-kafka
- ecmascript-6 - 作为开槽节点
- python - 如何使用 PIL 将图像从 HSV 转换为 RGB?
- scala - 注入构造函数时出错,com.datastax.driver.core.exceptions.NoHostAvailableException
- keras - 如何在 Keras 中向 Conv2D 层添加自定义“过滤器”
- apache-camel - 在 apache camel 中禁用自动重定向
- c++ - c++:用户定义类型的向量?
- java - 加入两个列表并更改特定数据