我有多个数据框,我想过滤每个数据框,以便每个df仅保留由单词“ Overall”组成的列。我有以下for循环,但它的效果不如手动完成[aka y15 = y15.filter(like ='Overall')]。
pit_dfs = [y15,y16,y17]
for i in pit_dfs:
i = i.filter(like='Overall')
可复制的示例:
y15 = pd.DataFrame({'Col1-Overall': ['a','b','c','d'],
'Col2': ['a','b','c','d'],
'Col3': ['a','b','c','d'],
'Col4': ['a','b','c','d']})
y16 = pd.DataFrame({'Col1-Overall': ['a','b','c','d'],
'Col2': ['a','b','c','d'],
'Col3': ['a','b','c','d'],
'Col4': ['a','b','c','d']})
y17 = pd.DataFrame({'Col1-Overall': ['a','b','c','d'],
'Col2': ['a','b','c','d'],
'Col3': ['a','b','c','d'],
'Col4': ['a','b','c','d']})
预期输出:
y15
+--------------+
| Col1-Overall |
+--------------+
| a |
+--------------+
| b |
+--------------+
| c |
+--------------+
| d |
+--------------+
y16
+--------------+
| Col1-Overall |
+--------------+
| a |
+--------------+
| b |
+--------------+
| c |
+--------------+
| d |
+--------------+
y17
+--------------+
| Col1-Overall |
+--------------+
| a |
+--------------+
| b |
+--------------+
| c |
+--------------+
| d |
+--------------+
我知道这是一个简单的例子,但是过去一个小时一直在研究Stack,但是找不到类似的例子。我想念什么?谢谢!
答案 0 :(得分:2)
有关Python for
循环,请参见this answer和this example。循环中的变量是 not 指针,因此您无需更改实际的数据帧。
您可以(我尚未测试过):
pit_dfs = [y15,y16,y17,y18,y19]
for idx in range(len(pit_dfs)):
pit_dfs[idx] = pit_dfs[idx].filter(like='Overall')
答案 1 :(得分:1)
这是另一种选择:
pit_dfs = [y15,y16,y17,y18,y19]
def filter_cols_like(df, like):
cols_not_like = [col for col in df.columns if like not in col]
df.drop(columns=cols_not_like,inplace=True)
for i in pit_dfs:
filter_cols_like(i,like='Overall')