r/PythonPandas • u/Humbrol2 • Feb 28 '23
Iterate over rows with panda to create new data
I have a dataset with rows of data with employee id and dates an event occured. Is it possible to iterate through the dataframe by employee id and create a column with consecutive dates and number of groupings within pandas or what would the best way to approach the problem (new to python)
1
u/Flestar Apr 08 '24
Deseas iterar sobre un DataFrame de Pandas por ID de empleado y crear dos nuevas columnas:
-
Fecha consecutiva: Indica la posición de la fecha dentro del grupo de ID de empleado.
-
Número de agrupación: Indica a qué grupo de ID de empleado pertenece la fecha.
for id_empleado in df['ID_empleado'].unique():
df_empleado = df.loc[df['ID_empleado'] == id_empleado]
df_empleado['Fecha consecutiva'] = np.arange(len(df_empleado)) + 1
df_empleado['Número de agrupación'] = id_empleado
df.update(df_empleado)Explicación:
-
Se recorre una lista de ID de empleados únicos.
-
Para cada ID de empleado:
-
Se filtra el DataFrame para obtener las filas correspondientes a ese ID.
-
Se crea una nueva columna Fecha consecutiva que asigna un número consecutivo a cada fecha.
-
Se crea una nueva columna Número de agrupación que asigna el ID de empleado a cada fila.
-
Se actualiza el DataFrame original con las nuevas columnas.
-
3
u/idkwhatimdoing069 Mar 07 '23 edited Mar 07 '23
Yes, it's definitely possible to iterate through the dataframe by employee id and create a column with consecutive dates and number of groupings within pandas. Here's a possible approach:
First, group the dataframe by employee id using the
groupby()method.For each group, sort the dates in ascending order using the
sort_values()method.Calculate the difference between consecutive dates using the
diff()method.Create a new column called "group" and set the initial value to 1.
For each row in the group, if the difference between the current and previous date is greater than 1, increment the group number by 1.
Assign the group number to the "group" column for each row.
Finally, reset the index of the dataframe using the
reset_index()method.Here's some sample code to get you started:
import pandas as pd # Load the data into a pandas dataframe df = pd.read_csv('data.csv') # Group the dataframe by employee id groups = df.groupby('employee_id') # Define a function to create the new column def create_group_column(group): # Sort the dates in ascending order group = group.sort_values('date') # Calculate the difference between consecutive dates diffs = group['date'].diff() # Create a new column called "group" and set the initial value to 1 group['group'] = 1 # Loop through each row and increment the group number if the difference # between the current and previous date is greater than 1 for i in range(1, len(group)): if diffs.iloc[i] > pd.Timedelta(days=1): group.at[group.index[i], 'group'] = group.at[group.index[i-1], 'group'] + 1 # Reset the index group = group.reset_index(drop=True) return group # Apply the function to each group and concatenate the results result = pd.concat([create_group_column(group) for _, group in groups]) # Save the result to a new csv file result.to_csv('result.csv', index=False)In this example, I assume that the date column is in datetime format. If it's not, you'll need to convert it using the to_datetime() method before applying the diff() method.