简体   繁体   English

如何跨多列循环类似的python pandas代码

[英]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. 我写了一段代码,从其中一个时间戳列中提取Month,hour,weekday。 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: 首先指定由日期时间填充的列,然后使用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 : 您可以像这样在参数中声明具有列名和df的函数:

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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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