How can you find when a value changes throughout every row in a data frame?How to drop rows of Pandas DataFrame whose value in certain columns is NaNHow to reset index in a pandas data frame?Pandas Datframe1 search for match in range of Dataframe2Change rows order pandas data framePython Pandas - Appending data from multiple data frames onto same row by matching primary identifier, leave blank if no results from that data frameMerging two CSV files into a Python data frame when the merge key string is not identicalChange stacking of dataframe in pandasCreate new dataframe using existing dataframe columns pandasComparing two pandas dataframes on column and the rowChanging x-axis labels in errorbar plot: no attribute 'get_xticklabels'
SOQL query WHERE filter by specific months
Is 'contemporary' ambiguous and if so is there a better word?
Why would a military not separate its forces into different branches?
Is an HNN extension of a virtually torsion-free group virtually torsion-free?
Where to draw the line between quantum mechanics theory and its interpretation(s)?
Can my 2 children, aged 10 and 12, who are US citizens, travel to the USA on expired American passports?
Will 700 more planes a day fly because of the Heathrow expansion?
Why did WWI include Japan?
Nested loops to process groups of pictures
Find magical solution to magical equation
Has the Hulk always been able to talk?
A factorization game
How in the world do I place line of text EVENLY between two horizontal tikz lines?
Feasibility of lava beings?
What to use instead of cling film to wrap pastry
Why do people keep telling me that I am a bad photographer?
History of the kernel of a homomorphism?
Can I use a Cat5e cable with an RJ45 and Cat6 port?
Where are the "shires" in the UK?
Why doesn't ever smooth vector bundle admits a line bundle?
Gerrymandering Puzzle - Rig the Election
What is the closest airport to the center of the city it serves?
Out of scope work duties and resignation
Side effects of Initiation by a Guru?
How can you find when a value changes throughout every row in a data frame?
How to drop rows of Pandas DataFrame whose value in certain columns is NaNHow to reset index in a pandas data frame?Pandas Datframe1 search for match in range of Dataframe2Change rows order pandas data framePython Pandas - Appending data from multiple data frames onto same row by matching primary identifier, leave blank if no results from that data frameMerging two CSV files into a Python data frame when the merge key string is not identicalChange stacking of dataframe in pandasCreate new dataframe using existing dataframe columns pandasComparing two pandas dataframes on column and the rowChanging x-axis labels in errorbar plot: no attribute 'get_xticklabels'
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
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'
python-3.x pandas jupyter-notebook
add a comment |
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'
python-3.x pandas jupyter-notebook
add a comment |
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'
python-3.x pandas jupyter-notebook
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'
python-3.x pandas jupyter-notebook
python-3.x pandas jupyter-notebook
asked Mar 23 at 1:42
jwrigjwrig
133
133
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
The following code logic might work for you case.
EDIT: Based on your comment, I modified the code so that all columns except the last one are checked.
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
Thank you very much for your response! That code makes a lot of sense. would there be a way to tweak it to make it so that it returns a value for every column? For instance return #smith continuing returning lost new " "
– jwrig
Mar 23 at 5:01
updated per your comment.
– jxc
Mar 23 at 15:29
That is exactly the result i was looking for. Thank you very much for your effort, patience and insight. I'm very new to python so you're showing me a lot of things that I will be looking deeper into as i expect to be using pandas regularly.
– jwrig
Mar 24 at 2:46
glad it helped and good luck to you with the new journey.
– jxc
Mar 24 at 2:59
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55309823%2fhow-can-you-find-when-a-value-changes-throughout-every-row-in-a-data-frame%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The following code logic might work for you case.
EDIT: Based on your comment, I modified the code so that all columns except the last one are checked.
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
Thank you very much for your response! That code makes a lot of sense. would there be a way to tweak it to make it so that it returns a value for every column? For instance return #smith continuing returning lost new " "
– jwrig
Mar 23 at 5:01
updated per your comment.
– jxc
Mar 23 at 15:29
That is exactly the result i was looking for. Thank you very much for your effort, patience and insight. I'm very new to python so you're showing me a lot of things that I will be looking deeper into as i expect to be using pandas regularly.
– jwrig
Mar 24 at 2:46
glad it helped and good luck to you with the new journey.
– jxc
Mar 24 at 2:59
add a comment |
The following code logic might work for you case.
EDIT: Based on your comment, I modified the code so that all columns except the last one are checked.
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
Thank you very much for your response! That code makes a lot of sense. would there be a way to tweak it to make it so that it returns a value for every column? For instance return #smith continuing returning lost new " "
– jwrig
Mar 23 at 5:01
updated per your comment.
– jxc
Mar 23 at 15:29
That is exactly the result i was looking for. Thank you very much for your effort, patience and insight. I'm very new to python so you're showing me a lot of things that I will be looking deeper into as i expect to be using pandas regularly.
– jwrig
Mar 24 at 2:46
glad it helped and good luck to you with the new journey.
– jxc
Mar 24 at 2:59
add a comment |
The following code logic might work for you case.
EDIT: Based on your comment, I modified the code so that all columns except the last one are checked.
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
The following code logic might work for you case.
EDIT: Based on your comment, I modified the code so that all columns except the last one are checked.
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
edited Mar 23 at 15:28
answered Mar 23 at 3:56
jxcjxc
1,5232310
1,5232310
Thank you very much for your response! That code makes a lot of sense. would there be a way to tweak it to make it so that it returns a value for every column? For instance return #smith continuing returning lost new " "
– jwrig
Mar 23 at 5:01
updated per your comment.
– jxc
Mar 23 at 15:29
That is exactly the result i was looking for. Thank you very much for your effort, patience and insight. I'm very new to python so you're showing me a lot of things that I will be looking deeper into as i expect to be using pandas regularly.
– jwrig
Mar 24 at 2:46
glad it helped and good luck to you with the new journey.
– jxc
Mar 24 at 2:59
add a comment |
Thank you very much for your response! That code makes a lot of sense. would there be a way to tweak it to make it so that it returns a value for every column? For instance return #smith continuing returning lost new " "
– jwrig
Mar 23 at 5:01
updated per your comment.
– jxc
Mar 23 at 15:29
That is exactly the result i was looking for. Thank you very much for your effort, patience and insight. I'm very new to python so you're showing me a lot of things that I will be looking deeper into as i expect to be using pandas regularly.
– jwrig
Mar 24 at 2:46
glad it helped and good luck to you with the new journey.
– jxc
Mar 24 at 2:59
Thank you very much for your response! That code makes a lot of sense. would there be a way to tweak it to make it so that it returns a value for every column? For instance return #smith continuing returning lost new " "
– jwrig
Mar 23 at 5:01
Thank you very much for your response! That code makes a lot of sense. would there be a way to tweak it to make it so that it returns a value for every column? For instance return #smith continuing returning lost new " "
– jwrig
Mar 23 at 5:01
updated per your comment.
– jxc
Mar 23 at 15:29
updated per your comment.
– jxc
Mar 23 at 15:29
That is exactly the result i was looking for. Thank you very much for your effort, patience and insight. I'm very new to python so you're showing me a lot of things that I will be looking deeper into as i expect to be using pandas regularly.
– jwrig
Mar 24 at 2:46
That is exactly the result i was looking for. Thank you very much for your effort, patience and insight. I'm very new to python so you're showing me a lot of things that I will be looking deeper into as i expect to be using pandas regularly.
– jwrig
Mar 24 at 2:46
glad it helped and good luck to you with the new journey.
– jxc
Mar 24 at 2:59
glad it helped and good luck to you with the new journey.
– jxc
Mar 24 at 2:59
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55309823%2fhow-can-you-find-when-a-value-changes-throughout-every-row-in-a-data-frame%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown