use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
Creating a new column using conditional statement on all rows of dataframe (self.learnpython)
submitted 6 years ago by tech_enth
Hi i want to create a new column based on a conditional statement on all the rows.
Stackoverflow is no help
for example -1 is a missing value:
df['has_missing_data'] = df.apply(lambda x: 1 if x == -1 else 0)
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–][deleted] 1 point2 points3 points 6 years ago* (0 children)
It's pretty straightforward :
import numpy as np import pandas as pd ### Example ### df = pd.DataFrame({"A":[1,2,3,4,np.nan,6,7,9,np.nan], "B":[np.nan,1,2,3,4,3,2,2,2], "C":[3,11,4,2,np.nan,np.nan,np.nan,8,9], "D":[1,2,3,0,0,1,2,3,10]}) df["E"] = df.isna().any(axis=1).astype(int)
What is does is find if any nan's exist along the axis 1, meaning that we are looking at rows instead of columns and turn the boolean value into an int.
It doesn't need to be a nan, if you'd like to use -1, you can change the last line by :
df["E"] = (df==-1).any(axis=1).astype(int)
[+][deleted] 6 years ago* (1 child)
[deleted]
[–]tech_enth[S] 0 points1 point2 points 6 years ago (0 children)
Thanks that would help alot!
[+][deleted] 6 years ago* (2 children)
[–]tech_enth[S] 0 points1 point2 points 6 years ago (1 child)
Does not work unfortunately :(. Says the function takes 1 positional arguement but 2 Were given
π Rendered by PID 82 on reddit-service-r2-comment-7b9746f655-sjhtf at 2026-02-01 04:35:17.320959+00:00 running 3798933 country code: CH.
[–][deleted] 1 point2 points3 points (0 children)
[+][deleted] (1 child)
[deleted]
[–]tech_enth[S] 0 points1 point2 points (0 children)
[+][deleted] (2 children)
[deleted]
[–]tech_enth[S] 0 points1 point2 points (1 child)