Issue
How do I filter columns of a data frame not containing a given string in their label?
DataFrame.filter allows, for example, to select all columns of a data frame whose label contain a provided string.
df = pd.DataFrame(
np.array(([1, 2, 3], [4, 5, 6])),
columns=['beat', 'meat', 'street']
)
df.filter(like="eat", axis=1) ### yields the columns "beat" and "meat".
Is there a way to revert this logic, so that I may only keep those columns not containing "eat"? Alternatively: Is there a way to drop columns containing "eat"?
Solution
Use regex
parameter:
print (df.filter(regex=r'^(?!.*eat).*$'))
Answered By - jezrael
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.