Pandas rename columns with wildcard

2019-07-17 04:08发布

问题:

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)