To subset columns of a dataframe based on whether they contain a string in Python, you first need to import the pandas
library.
Then you can use the .filter()
method along with a lambda function to check if each column name contains the desired string. Here's an example:
main.py309 chars11 lines
In this example, the resulting df_subset
dataframe would only contain the Address
column, since the other columns do not contain the string 'dd'.
Note that the axis=1
argument specifies that we are filtering columns, not rows.
gistlibby LogSnag