简体   繁体   中英

How to loop similar python pandas code across multiple columns

I have written a code to extract Month, Hour, Weekday from one of the timestamp columns. i would like to apply the same code across other timestamp columns on my data without rewritting the code.

df['closed_at'] = pd.to_datetime(df['closed_at'], errors='coerce')
df['closed_at - Month-Year'] = df['closed_at'].dt.to_period('M')
df['closed_at - Weekday Num'] = df['closed_at'].dt.dayofweek + 1
df['closed_at - Weekday'] = df['closed_at'].dt.weekday_name
df['closed_at - Weekday Combo'] = df['closed_at - Weekday Num'].astype(str)+'-'+df['closed_at - Weekday']
df['closed_at - Hour Num'] = df['closed_at'].dt.hour

First specify columns filled by datetimes and create new columns in loop with f-string s:

cols = ['closed_at', 'another date col']

for x in cols:
    incident_data[x] = pd.to_datetime(incident_data[x], errors='coerce')
    incident_data[f'{x} - Month-Year'] = incident_data[x].dt.to_period('M')
    incident_data[f'{x} - Weekday Num'] = incident_data[x].dt.dayofweek + 1
    incident_data[f'{x} - Weekday'] = incident_data[x].dt.weekday_name
    incident_data[f'{x} - Weekday Combo'] = (incident_data[f'{x} - Weekday Num'].astype(str)+
                                             '-'+incident_data[f'{x} - Weekday'])
    incident_data[f'{x} - Hour Num'] = incident_data[x].dt.hour

You can declare a function with the column name and the df in parameters like this :

def transformation(df,column_name):
    df[column_name] = pd.to_datetime(df[column_name], errors='coerce')
    df['closed_at - Month-Year'] = df[column_name].dt.to_period('M')
    df['closed_at - Weekday Num'] = df[column_name].dt.dayofweek + 1
    df['closed_at - Weekday'] = df[column_name].dt.weekday_name
    df['closed_at - Weekday Combo'] = df['closed_at - Weekday Num'].astype(str)+'-'+df['closed_at - Weekday']
    df['closed_at - Hour Num'] = df[column_name].dt.hour
    return df

Then you can iterate on different columns with a list of names for instance.

df = transformation(df,'closed_at')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM