Assign values to a grouped by dataframe

given this dataframe how can I find where if rows are grouped by first name and last name, the row with type as ‘CA’ get its Value column set to the row with type as ‘GCA’ value? so in this example the first row Alice, Johnson, CA, 25 will have its value changed from 25… Read More Assign values to a grouped by dataframe

Create new dataframe based on distinct combinations of column values in Python

I have a pandas dataframe: d = {‘col1’: [‘Date1’, ‘Date1’, ‘Date1’, ‘Date2’, ‘Date2’, ‘Date2’, ‘Date3’, ‘Date3’, ‘Date3’, ‘Date4’, ‘Date4’, ‘Date4’], ‘col2’: [‘Date2’, ‘Date3’, ‘Date4’, ‘Date1’, ‘Date3’, ‘Date4’, ‘Date1’, ‘Date2’, ‘Date4’, ‘Date1’, ‘Date2’, ‘Date3’]} df = pd.DataFrame(data=d) How do I get a unique list of combinations of the values in the columns, like this? I have… Read More Create new dataframe based on distinct combinations of column values in Python

Calculate % in Pandas pivot_table?

I have a table with data for who viewed a page and who clicked on it. The following code gets me the pivot table below: # users who clicked, by hour and weekday pct = df.pivot_table(columns=[‘weekday’],index=[‘hour’], values=[‘users_who_clicked’,’users_who_viewed’], aggfunc= sum, fill_value=0, margins=True) pct pivot table I’ve been googling and searching here for a solid hour but… Read More Calculate % in Pandas pivot_table?

Groupby one column, if the date column are the same, fill in the missing values in the numerical column

Assuming for the dataframe df as follows: date actual_value fitted_value predicted_value code 0 2023/8/31 NaN NaN 520.994413 LX0301 1 2023/9/30 NaN NaN 580.967973 LX0301 2 2023/10/31 NaN NaN 650.392867 LX0301 3 2023/8/31 471.459992 520.027310 NaN LX0301 4 2023/9/30 NaN NaN 531.199547 LX0301 5 2023/10/31 NaN NaN 600.053484 LX0301 6 2023/8/31 471.459992 511.902229 NaN LX0301 7… Read More Groupby one column, if the date column are the same, fill in the missing values in the numerical column

Can I use pandas `groupby().apply()` to do stuff that doesn't return a value?

Suppose I have a function that does something based on a DataFrame but doesn’t return a value. Something like, writing to a log file, or uploading to a database. def some_func_no_return_value(df): # Do something based on df return # Doesn’t return anything I have some code that does this: for key, df_group in df.groupby(some_column): some_func_no_return_value(df_group)… Read More Can I use pandas `groupby().apply()` to do stuff that doesn't return a value?