I'm trying to fill values in one column from two other columns based on the values in a fourth column.
I have a pandas dataframe with four columns: A, B, C, D
df_copy = df.copy()
for i, row in df.iterrows():
if 'Test' in row.D:
df_copy.loc[i, 'A'] = row.B
elif 'Other' in row.D:
df_copy.loc[i, 'A'] = row.C
This works, but is very slow. Is there a more efficient way?
You can use 'boolean indexing' for this instead of iterating over all rows:
df_copy.loc[df['D']=='Test', 'A'] = df['B']
df_copy.loc[df['D']=='Other', 'A'] = df['C']
If you know that column D only consists of these two values, it can even shorter:
df_copy['A'] = df['B']
df_copy.loc[df['D']=='Other', 'A'] = df['C']
If you want to have the same as the in
operator to test if that substring is in the column, you can do:
df['D'].str.contains('Other')
to become the boolean values instead of the df['D']=='Other'
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments