Pandas rename columns with wildcard

半城伤御伤魂 提交于 2019-12-10 23:14:35

问题


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.


回答1:


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)



回答2:


You can use replace by dict, for wildcard use .* and for start of string ^:

d = {'^Temperatur.*': 'Temperatur', 'Luftdruck[hPa]': 'Luftdruck'}
df.columns = df.columns.to_series().replace(d, regex=True)

Sample:

cols = ['Datum',   'Zeit',    'Temperatur[°C]', 'Luftdruck' ,  'Windgeschwindigkeit[m/s]',
        'Windrichtung[Grad]',  'Relative Luftfeuchtigkeit[%]',   ' Globalstrahlung[W/m²]']

df = pd.DataFrame(columns=cols)
print (df)
Empty DataFrame
Columns: [Datum, Zeit, Temperatur[°C], Luftdruck, Windgeschwindigkeit[m/s], 
          Windrichtung[Grad], Relative Luftfeuchtigkeit[%],  Globalstrahlung[W/m²]]
Index: []

d = {'^Temperatur.*': 'Temperatur', 'Luftdruck.*': 'Luftdruck'}
df.columns = df.columns.to_series().replace(d, regex=True)
print (df)

Empty DataFrame
Columns: [Datum, Zeit, Temperatur, Luftdruck, Windgeschwindigkeit[m/s], 
          Windrichtung[Grad], Relative Luftfeuchtigkeit[%],  Globalstrahlung[W/m²]]
Index: []



回答3:


You may prepare a function for renaming you columns:

rename_columns(old_name):
    if old_name == 'Temperatur':
        new_name = old_name + whichever_you_wants    # may be another function call
    elif old_name == 'Luftdruck':
        new_name = 'Luftdruck[hPa]'
    else:
        new_name = old_name
    return new_name

and then use the .rename() method with that function as a parameter:

wetterdaten.rename(columns=rename_columns, inplace=True)


来源:https://stackoverflow.com/questions/46706850/pandas-rename-columns-with-wildcard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!