我在Python Pandas中有DataFrame,如下所示(我的实际df有更多的列):
Input data:个
COL1 | col2 | col3
------|--------|-------
X | 11 | 2021
Y | 22 | 1990
Requirements:个
如果存在List中的列,我需要在DataFrame中将除以下列表中的列名之外的每个列名放在较高位置:
list_not_to_up = ["col2", "col55"]
我的代码是:
df.columns = [x.upper() if x not in df[list_not_to_up].columns else x for x in df.columns]
尽管如此,我还是有错:KeyError: "['col55'] not in index"
Desire output:个
COL1 | col2 | COL3
------|--------|-------
X | 11 | 2021
Y | 22 | 1990
如果在Python Pandas中的DataFrame中当然存在List中的列,那么如何修改我的代码以使DataFrame中除List中的列之外的所有列都位于上方?