import pandas as pd
# sample dataframe
df = pd.DataFrame({'foo': [1, 2, 3], 'bar': [4, 5, 6], 'ber': [7, 8, 9]})
# sample list of strings
mylist = ['oo', 'ba']
# join the list to a single string
matches = '|'.join(mylist)
# use regex to filter the columns based on the string
df_out = df.filter(regex=matches)
1条答案
按热度按时间7xzttuei1#
您可以将
df.filter
与regex
一起使用来完成此操作。