pandas 使用通配符重命名列

编程入门 行业动态 更新时间:2024-10-24 19:26:53
本文介绍了 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 使用通配符重命名列

本文发布于:2023-10-16 16:06:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1498062.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:通配符   重命名   pandas

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!