PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, August 14, 2022

[FIXED] How can I export single value out of pandas ataframe using where function?

 August 14, 2022     output, pandas, python, where-clause     No comments   

Issue

my pandas dataframe is like this:

          a        b                         c
0         10       hi                       sth
1         300      hello                    0
2         2157     bye                      any

my query is like:

df[a].where(df['b'] == 'hi')

the result shows all the column a with the value for where b condition is true. My question is how can I have a single value (as for my case only one row is true) as the result istead of a list, Which in my example is "10"


Solution

You can use loc and squeeze:

df.loc[df['b'].eq('hi'), 'a'].squeeze()

output: 10

Note that in case you have more than one match, you will get a Series as output.



Answered By - mozway
Answer Checked By - Willingham (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing