-2

How do I print two columns, where one column has a condition? I want to print all the cities that were first time disclosing, and exclude those cities that have disclosed previously. I've tried combinations similar to the below.

print(US20_df[{'City'} {'First Time Discloser'] == 'Yes'}])

print(US20_df['City',('First Time Discloser' == 'Yes')])


City First Time Discloser
Dallas           Yes
Chicago           No
Memphis           No
Greg
  • 31
  • 2
  • 8
  • It would help if you posted (as text) a portion of your dataframe – piterbarg Nov 07 '20 at 20:22
  • Does this answer your question? [How to select rows from a DataFrame based on column values](https://stackoverflow.com/questions/17071871/how-to-select-rows-from-a-dataframe-based-on-column-values) – Bill Huang Nov 07 '20 at 21:13

1 Answers1

0

If you do this

US20_df = pd.DataFrame({'City':['Dallas','Chicago', 'Memphis'], 'First Time Discloser' : ['Yes', 'No', 'No']})
US20_df[US20_df['First Time Discloser']=='Yes'] 

you should get


    City    First Time Discloser
0   Dallas  Yes
piterbarg
  • 8,089
  • 2
  • 6
  • 22
  • Hi yes that worked and printed those out thank you. That code returns all the other columns as well. How would I just return those two columns? – Greg Nov 07 '20 at 20:54
  • 1
    like this `US20_df[US20_df['First Time Discloser']=='Yes'] [['City','First Time Discloser']]` – piterbarg Nov 07 '20 at 21:22