I have dataframe Polars with a lot of columns. I need to create new columnt with the result of my own function
df = pl.DataFrame({
'Stock_name': ['reserve', 'fullfilment', 'ntl', 'ntl'],
'doc_type': ['sales', 'moving', 'sales', 'corr'],
})
def doc_check(row):
if (row['Stock_name'] == 'reserve') or (row['Stock_name'] == 'ntl'):
if row['doc_type'] == 'sales':
doc = 'sales'
else:
doc = row['doc_type']
else:
doc = row['doc_type']
return doc
df = df.apply(doc_check).alias('doc_type_new')
df['doc_type_new'] = df.apply(doc_check)
But I received an error “TypeError: tuple indices must be integers or slices, not str”. Where is an error in my code?
New contributor
Masik is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.