0
$\begingroup$

I have a list (sub_indices) containing dataframe index values. Using these index values, I need to make changes in another dataframe (main_df). Changes must be done on these index values that are in the list.

What should be the best way to solve this task? I'v tried lambda and map functions but am getting an error: "AttributeError: ("'list' object has no attribute 'apply'", 'occurred at index 0')".

Seems that lambda function cannot be called on a list.

def function_1(new_value):
    sub_indices = sub_df.index.values.tolist()
    sub_indices.apply(lambda x: function_2(x))

def function_2(df_indices):
    # Replacement that needs to be done. 'indes_value' is the next element from 'sub_indices' list.
    main_df.at[indes_value, 'column_name'] = "new value"
$\endgroup$

1 Answer 1

1
$\begingroup$

You can simply use df.loc to achieve what you want. Assuming indice is the list of indices where you want to make the changes, do:

main_df.loc[indice, 'column_name'] = new_value
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.