本文介绍了 pandas 使用通配符重命名列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的df看起来像这样:
My df looks like this:
Datum Zeit Temperatur[°C] Luftdruck Windgeschwindigkeit[m/s] Windrichtung[Grad] Relative Luftfeuchtigkeit[%] Globalstrahlung[W/m²]现在我想重命名像这样的列:#
Now i want to rename the columns like this:#
wetterdaten.rename(columns={'Temperatur%': 'Temperatur', 'Luftdruck[hPa]': 'Luftdruck'}, inplace=True)其中%是通配符. 但是,它当然不会像这样工作.
Where % is a wildcard. But of course it will not work like this.
在日志数据中,列名的开头始终相同, 但是结局是暂时改变的.
The beginning of the column name is always the same in the log data, but the ending is temporally changing.
推荐答案您可以过滤列并获取名称:
You can filter the columns and fetch the name:
wetterdaten.rename(columns={wetterdaten.filter(regex='Temperatur.*').columns[0]: 'Temperatur', 'wetterdaten.filter(regex='Luftdruck.*').columns[0]': 'Luftdruck'}, inplace=True)更多推荐
pandas 使用通配符重命名列
发布评论