-2

enter image description here I am trying to do a filter within a column on a csv file. For the county_name column I want only the texas states. Similarly to how in excel I would filter to have the counties containing tx for that column. Any help on how to do so?

enter image description here

  • 3
    Hi, Welcome to SO please read how to post question guidelines or else your question will be downvoted. https://stackoverflow.com/help/how-to-ask. Remove all the images and make them like tables using markdown syntax. – coldy Feb 01 '22 at 17:25
  • 2
    Presumably you are using Pandas so try searching existing pandas questions on how to select rows based on column values. Probably https://stackoverflow.com/questions/31595146/how-do-i-use-within-in-operator-in-a-pandas-dataframe – Iguananaut Feb 01 '22 at 17:27
  • 1
    detailed answer here https://stackoverflow.com/a/55335207/14973743 – Anand Sowmithiran Feb 01 '22 at 17:30
  • Update: You might also want `table.county_name.str.contains('tx')` or something like that. – Iguananaut Feb 01 '22 at 17:30
  • @AnandSowmithiran suggestion is actually btter for a duplicate – Iguananaut Feb 01 '22 at 17:31
  • Lots of ways to go on this problem. If you don't want to install any new modules, then you can look at the built-in [csv reader](https://docs.python.org/3/library/csv.html). If you find yourself using tabular data a lot, it might pay to learn to use the [Pandas](https://docs.python.org/3/library/csv.html) module. Finally, there are a number of modules that can be used to manipulate Excel files. – bfris Feb 01 '22 at 17:32

1 Answers1

0
new_df =df.loc[df['county_name'].str.split(",")[1] == 'tx'] 

You can filter county_name col for tx and store it in a new_df like excel

agastya teja
  • 629
  • 2
  • 9
  • 18