避免在修改它们时重复相同的数据框列名

时间:2019-06-26 14:22:56

标签: python python-3.x dataframe

我有一个超过30列的数据框。我正在对特定列进行各种修改,并希望找到一种避免总是列出特定列的方法。有捷径吗?

例如:

[4, -3, 10, 0]

我可以只定义一次“ SpecificColumns”一词,然后将其粘贴到这里吗?

matrix_bus_filled.loc[matrix_bus_filled['FNR'] == 'AB1122', ["Ice", "Tartlet", "Pain","Fruit","Club","Focaccia","SW of Month","Salad + Dressing","Planchette + bread","Muffin"]] = matrix_bus_filled[matrix_bus_filled['FNR'] == 'AB1120'][["Ice", "Tartlet", "Pain","Fruit","Club","Focaccia","SW of Month","Salad + Dressing","Planchette + bread","Muffin"]].values

还有这里

matrix_bus_filled.loc[matrix_bus_filled['FNR'] == 'AB1122', ["SpecificColumns"]] = matrix_bus_filled[matrix_bus_filled['Flight Number'] == 'AB1120'][["SpecificColumns]].values

1 个答案:

答案 0 :(得分:0)

只需定义一个列表并使用它来调用列。

specific_columns = ["Ice", "Tartlet", "Pain","Fruit","Club","Focaccia","SW of Month","Salad + Dressing","Planchette + bread","Muffin"]

matrix_bus_filled[specific_columns] = matrix_bus_filled[specific_columns].apply(scale, axis=1)