1

I want to select A,B,C columns row where D columns value is 1. in sql i can use this :

select A,B,C
from table
where D = 1

How can i convert this in pandas dataframe?

2 Answers2

3

Use loc:

print(df.loc[df['D'] == 1, ['A', 'B', 'C']])
U13-Forward
  • 69,221
  • 14
  • 89
  • 114
1

you can use another way and don't use loc like below:

df[df['D']==1][['A','B','C']]
I'mahdi
  • 23,382
  • 5
  • 22
  • 30
  • 1
    @SädnänMøhøsïn if this is correct and help you please read this link : https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – I'mahdi Sep 05 '21 at 08:27
  • Well this is less efficient tho... – U13-Forward Sep 05 '21 at 08:39
  • My answer with `loc` is much more efficient because that would extract from each row instead of constructing the whole dataframe then getting the specific columns @SädnänMøhøsïn – U13-Forward Sep 05 '21 at 08:40
  • @U12-Forward two answer work as same. this is not good for getting `accept` say Contrary to reality. two answer work this : `extract from each row instead of constructing the whole dataframe` – I'mahdi Sep 05 '21 at 09:25
  • @SädnänMøhøsïn no matter you `accept` which answer but both answers work as same. I mention this that you know. both answer first find df['D']==1 then return column `A,B,C` exactly what you want and say in your question. – I'mahdi Sep 05 '21 at 09:28