I am a newbie and trying to figure out how to correctly use the .loc function in pandas for slicing a dataframe. Any help is greatly appreciated.
The code is:
df1['Category'] = df[key_column].apply(lambda x: process_df1(x, 'category'))
where df1 is a dataframe, key_column is a specific column identified to be operated upon process_df1 is a function defined to run on df1.
The problem is I am trying to avoid the error: "A value is trying to be set on a copy of a slice from a DataFrame. Try using
.loc[row_indexer,col_indexer] = value instead"
I don't want to ignore / suppress the warnings or set `pd.options.mode.chained_assignment = None.
Is there an alternative besides these 2?
I have tried using
df.loc[df1['Category'] = df[key_column].apply(lambda x: process_df1(x, 'category'))]
but it still produces the same error. Am I using the .loc incorrectly?
Apologies if it is a confusing question.
df1 = df[:break_index] df2 = df[break_index:]
Thank you.