Pandas是Python中强大的数据分析库,如果你想高效处理数据,熟练掌握DataFrame的用法是必不可少的。本文介绍3种筛选DataFrame中包含特定字符串的列的方法。
我们以一个简单的DataFrame为例:
col1 col2 col3 col4 col5
0 1 4 7 10 13
1 2 5 8 11 14
2 3 6 9 12 15
1. 选择包含"col"的列
使用`filter`和`like`参数:
df.filter(like='col')
这会返回包含"col"字符串的全部列:
col1 col2 col3 col4 col5
0 1 4 7 10 13
1 2 5 8 11 14
2 3 6 9 12 15
2. 选择末尾为特定字符串的列
继续使用`filter`和`like`参数:
df.filter(like='4')
这会返回末尾包含"4"的列:
co