I am currently trying to create a new column within a polars dataframe (df). Within my df, there are many many rows, and within this new column I only want my existing list to populate wherever certain conditions are met.
I know that in pandas, I could do something like the following (where ‘output’ is my new column and assuming list is the same length as the filtered df) :
df = pd.DataFrame({
'cond1': [-1, 0, 1, 2, 2, 3, 4, 5],
'cond2': ['No', 'No', 'No', 'Yes','Yes', 'Yes', 'Yes', 'Yes']
})
conditions = ((df['cond1']>=1) & (df['cond2'] == 'Yes'))
list_new = [1,2,3,4,5]
df.loc[conditions, 'output'] = list_new
How would I do this same thing but within polars? I want the total number of rows to stay the same, I just want to update the new column wherever the conditions are met
I tried a few variations of the following but nothing has seemed to work:
df_polars = df_polars.with_columns(pl.when(conditions).then(list_new).alias('output')
Datawiz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.