Python 数据帧列选择
我使用Pandas从数据帧olddf中选择列。假设变量名为“a”、“b”、“c”、“starswith1”、“startswith2”、“startswith3”、“startswith10” 我的方法是创建一个具有公共起始值的所有变量的列表Python 数据帧列选择,python,pandas,Python,Pandas,我使用Pandas从数据帧olddf中选择列。假设变量名为“a”、“b”、“c”、“starswith1”、“startswith2”、“startswith3”、“startswith10” 我的方法是创建一个具有公共起始值的所有变量的列表 filter_col = [col for col in list(health) if col.startswith('startswith')] 然后我想按名称选择该列表中的列以及其他列,这样我就不必全部键入。但是,这不起作用: newdf
filter_col = [col for col in list(health) if col.startswith('startswith')]
然后我想按名称选择该列表中的列以及其他列,这样我就不必全部键入。但是,这不起作用:
newdf = olddf['a','b',filter_col]
这也不是:
newdf = olddf[['a','b'],filter_col]
我是个新手,所以这可能很简单。这不起作用的原因是因为我不正确地混合了一个列表
谢谢。使用
newdf = olddf[['a','b']+filter_col]
由于添加列表会连接它们:
In [264]: ['a', 'b'] + ['startswith1']
Out[264]: ['a', 'b', 'startswith1']
或者,您可以使用: 使用 由于添加列表会连接它们:
In [264]: ['a', 'b'] + ['startswith1']
Out[264]: ['a', 'b', 'startswith1']
或者,您可以使用: