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
一、选择包含"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
二、选择末尾字符为"4"的列
还是使用`filter`和`like`参数:
df.filter(like='4')
这会返回末尾包含"4"的列:
col4
0 10
1 11
2 12