{"id":51944,"date":"2024-04-15T23:32:05","date_gmt":"2024-04-15T23:32:05","guid":{"rendered":"https:\/\/exam.pscnotes.com\/mcq\/?p=51944"},"modified":"2024-04-15T23:32:05","modified_gmt":"2024-04-15T23:32:05","slug":"in-pandas-which-method-is-used-to-create-a-new-column-based-on-conditional-logic-applied-to-existing-columns","status":"publish","type":"post","link":"https:\/\/exam.pscnotes.com\/mcq\/in-pandas-which-method-is-used-to-create-a-new-column-based-on-conditional-logic-applied-to-existing-columns\/","title":{"rendered":"In Pandas, which method is used to create a new column based on conditional logic applied to existing columns?"},"content":{"rendered":"<p>[amp_mcq option1=&#8221;melt()&#8221; option2=&#8221;df[&#8216;new_column&#8217;] = np.where(&#8230;)&#8221; option3=&#8221;sort_values()&#8221; option4=&#8221;melt()&#8221; correct=&#8221;option2&#8243;]<!--more--><\/p>\n<p>The correct answer is B. df[&#8216;new_column&#8217;] = np.where(&#8230;).<\/p>\n<p>The <code>np.where()<\/code> function takes three arguments: a condition, a value to return if the condition is true, and a value to return if the condition is false. In this case, the condition is a boolean expression that compares the values in two columns. If the condition is true, the value in the <code>new_column<\/code> will be the value in the first column. If the condition is false, the value in the <code>new_column<\/code> will be the value in the second column.<\/p>\n<p>The <code>melt()<\/code> function converts a DataFrame from a long format to a wide format. In a long format DataFrame, each row represents a single observation, and each column represents a variable. In a wide format DataFrame, each row represents a single variable, and each column represents a single observation.<\/p>\n<p>The <code>sort_values()<\/code> function sorts a DataFrame by the values in a specified column.<\/p>\n<p>Here is an example of how to use the <code>np.where()<\/code> function to create a new column based on conditional logic applied to existing columns:<\/p>\n<p>&#8220;`<br \/>\nimport pandas as pd<\/p>\n<p>df = pd.DataFrame({&#8216;A&#8217;: [1, 2, 3], &#8216;B&#8217;: [4, 5, 6]})<\/p>\n<p>df[&#8216;new_column&#8217;] = np.where(df[&#8216;A&#8217;] &gt; 2, &#8216;greater than 2&#8217;, &#8216;less than or equal to 2&#8217;)<\/p>\n<p>print(df)<\/p>\n<p>A  B  new_column<br \/>\n0  1  4  greater than 2<br \/>\n1  2  5  less than or equal to 2<br \/>\n2  3  6  less than or equal to 2<br \/>\n&#8220;`<\/p>\n<p>In this example, the <code>new_column<\/code> is created by comparing the values in the <code>A<\/code> column to the value 2. If the value in the <code>A<\/code> column is greater than 2, the value in the <code>new_column<\/code> is <code>greater than 2<\/code>. If the value in the <code>A<\/code> column is less than or equal to 2, the value in the <code>new_column<\/code> is <code>less than or equal to 2<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>[amp_mcq option1=&#8221;melt()&#8221; option2=&#8221;df[&#8216;new_column&#8217;] = np.where(&#8230;)&#8221; option3=&#8221;sort_values()&#8221; option4=&#8221;melt()&#8221; correct=&#8221;option2&#8243;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[737],"tags":[],"class_list":["post-51944","post","type-post","status-publish","format-standard","hentry","category-data-analysis-with-python","no-featured-image-padding"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v22.2 (Yoast SEO v23.3) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>In Pandas, which method is used to create a new column based on conditional logic applied to existing columns?<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/exam.pscnotes.com\/mcq\/in-pandas-which-method-is-used-to-create-a-new-column-based-on-conditional-logic-applied-to-existing-columns\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"In Pandas, which method is used to create a new column based on conditional logic applied to existing columns?\" \/>\n<meta property=\"og:description\" content=\"[amp_mcq option1=&#8221;melt()&#8221; option2=&#8221;df[&#8216;new_column&#8217;] = np.where(&#8230;)&#8221; option3=&#8221;sort_values()&#8221; option4=&#8221;melt()&#8221; correct=&#8221;option2&#8243;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/exam.pscnotes.com\/mcq\/in-pandas-which-method-is-used-to-create-a-new-column-based-on-conditional-logic-applied-to-existing-columns\/\" \/>\n<meta property=\"og:site_name\" content=\"MCQ and Quiz for Exams\" \/>\n<meta property=\"article:published_time\" content=\"2024-04-15T23:32:05+00:00\" \/>\n<meta name=\"author\" content=\"rawan239\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"rawan239\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"In Pandas, which method is used to create a new column based on conditional logic applied to existing columns?","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/exam.pscnotes.com\/mcq\/in-pandas-which-method-is-used-to-create-a-new-column-based-on-conditional-logic-applied-to-existing-columns\/","og_locale":"en_US","og_type":"article","og_title":"In Pandas, which method is used to create a new column based on conditional logic applied to existing columns?","og_description":"[amp_mcq option1=&#8221;melt()&#8221; option2=&#8221;df[&#8216;new_column&#8217;] = np.where(&#8230;)&#8221; option3=&#8221;sort_values()&#8221; option4=&#8221;melt()&#8221; correct=&#8221;option2&#8243;]","og_url":"https:\/\/exam.pscnotes.com\/mcq\/in-pandas-which-method-is-used-to-create-a-new-column-based-on-conditional-logic-applied-to-existing-columns\/","og_site_name":"MCQ and Quiz for Exams","article_published_time":"2024-04-15T23:32:05+00:00","author":"rawan239","twitter_card":"summary_large_image","twitter_misc":{"Written by":"rawan239","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/exam.pscnotes.com\/mcq\/in-pandas-which-method-is-used-to-create-a-new-column-based-on-conditional-logic-applied-to-existing-columns\/","url":"https:\/\/exam.pscnotes.com\/mcq\/in-pandas-which-method-is-used-to-create-a-new-column-based-on-conditional-logic-applied-to-existing-columns\/","name":"In Pandas, which method is used to create a new column based on conditional logic applied to existing columns?","isPartOf":{"@id":"https:\/\/exam.pscnotes.com\/mcq\/#website"},"datePublished":"2024-04-15T23:32:05+00:00","dateModified":"2024-04-15T23:32:05+00:00","author":{"@id":"https:\/\/exam.pscnotes.com\/mcq\/#\/schema\/person\/5807dafeb27d2ec82344d6cbd6c3d209"},"breadcrumb":{"@id":"https:\/\/exam.pscnotes.com\/mcq\/in-pandas-which-method-is-used-to-create-a-new-column-based-on-conditional-logic-applied-to-existing-columns\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/exam.pscnotes.com\/mcq\/in-pandas-which-method-is-used-to-create-a-new-column-based-on-conditional-logic-applied-to-existing-columns\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/exam.pscnotes.com\/mcq\/in-pandas-which-method-is-used-to-create-a-new-column-based-on-conditional-logic-applied-to-existing-columns\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/exam.pscnotes.com\/mcq\/"},{"@type":"ListItem","position":2,"name":"mcq","item":"https:\/\/exam.pscnotes.com\/mcq\/category\/mcq\/"},{"@type":"ListItem","position":3,"name":"Data science","item":"https:\/\/exam.pscnotes.com\/mcq\/category\/mcq\/data-science\/"},{"@type":"ListItem","position":4,"name":"Data analysis with python","item":"https:\/\/exam.pscnotes.com\/mcq\/category\/mcq\/data-science\/data-analysis-with-python\/"},{"@type":"ListItem","position":5,"name":"In Pandas, which method is used to create a new column based on conditional logic applied to existing columns?"}]},{"@type":"WebSite","@id":"https:\/\/exam.pscnotes.com\/mcq\/#website","url":"https:\/\/exam.pscnotes.com\/mcq\/","name":"MCQ and Quiz for Exams","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/exam.pscnotes.com\/mcq\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/exam.pscnotes.com\/mcq\/#\/schema\/person\/5807dafeb27d2ec82344d6cbd6c3d209","name":"rawan239","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/exam.pscnotes.com\/mcq\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/761a7274f9cce048fa5b921221e7934820d74514df93ef195a9d22af0c1c9001?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/761a7274f9cce048fa5b921221e7934820d74514df93ef195a9d22af0c1c9001?s=96&d=mm&r=g","caption":"rawan239"},"sameAs":["https:\/\/exam.pscnotes.com"],"url":"https:\/\/exam.pscnotes.com\/mcq\/author\/rawan239\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/exam.pscnotes.com\/mcq\/wp-json\/wp\/v2\/posts\/51944","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/exam.pscnotes.com\/mcq\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/exam.pscnotes.com\/mcq\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/exam.pscnotes.com\/mcq\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/exam.pscnotes.com\/mcq\/wp-json\/wp\/v2\/comments?post=51944"}],"version-history":[{"count":0,"href":"https:\/\/exam.pscnotes.com\/mcq\/wp-json\/wp\/v2\/posts\/51944\/revisions"}],"wp:attachment":[{"href":"https:\/\/exam.pscnotes.com\/mcq\/wp-json\/wp\/v2\/media?parent=51944"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/exam.pscnotes.com\/mcq\/wp-json\/wp\/v2\/categories?post=51944"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/exam.pscnotes.com\/mcq\/wp-json\/wp\/v2\/tags?post=51944"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}