0

I'm trying to copy all of the values across a subset of rows into a new column using the apply function, but it seems to just copying the entire dataframe range. I'm receiving that subset of the dataframe as a result, though I'm expecting df.loc[index, 'consolidated_commentary'] to contain a concatenated version of the the text in the columns contained within all_commentary_columns

My code is:

 for index, row in df[all_commentary_columns].iterrows():
if pd.isna(row).prod():
    df.loc[index, 'new_col'] = 'good'
else:
    df.loc[index, 'new_col'] = 'bad'
    df.loc[index, 'consolidated_commentary'] = df[all_commentary_columns].apply(lambda x: x.loc[all_commentary_columns], axis=1)
Gregory
  • 315
  • 1
  • 3
  • 13

1 Answers1

0

I pulled this from the answer I referenced in the comments:

df.loc[index, 'consolidated_commentary'] = df.loc[index, all_commentary_columns].astype(str).apply(lambda x: ' '.join(' '.join(x).split()),axis=1)
Pilgrim
  • 88
  • 1
  • 8