A function to return only certain columns with certain string - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Data Science (https://python-forum.io/forum-44.html) +--- Thread: A function to return only certain columns with certain string (/thread-28542.html) |
A function to return only certain columns with certain string - illmattic - Jul-23-2020 Hello, I have this data set with 11 columns: print(US_data) I'm trying to find/work out a function that can be used so that only the columns with 'YY' are returned. I assume it would be a if statement but I haven't been able to work it out.Any help is appreciated. RE: A function to return only certain columns with certain string - scidam - Jul-24-2020 if US_data is a Pandas dataframe, you can filter columns by name, e.g. cols_wanted = [col for col in US_data.columns if col.endswith('YY')] . Finally, you can select these columns, e.g. US_data[cols_wanted] . Probably, if 'YY' in col will be more suitable condition.
RE: A function to return only certain columns with certain string - illmattic - Jul-24-2020 (Jul-24-2020, 02:23 AM)scidam Wrote: if Thanks! |