pandas 使用通配符重命名列 [英] Pandas rename columns with wildcard

查看:119
本文介绍了 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 使用通配符重命名列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆