0

My question is similar to this one: Combine two columns of text in dataframe in pandas/python

However, I want to combine multiple columns, some of which are text and some are non-text. Moreover, I would like to do it in a loop, row by row because I need to preprocess the resulting text

I tried:

 for i in range(len(df)):
      text = df.loc[i, text_cols].apply(lambda x: ' '.join(str(x)))

text_cols is a parameter.

But it seems the result is not a text but an array of chars...

Update: It seems the following solution works:

  text = ''
  for col in text_cols:
    text += ' ' + str(df.loc[i,col])

I wonder if there is a more fancy solution for it.

Ahmad
  • 8,811
  • 11
  • 76
  • 141
  • Can you show some sample data? I have posted a answer where you can iterate row by row and do the pre processing. But I will need to have a look at your df to help you. Please post df.head() here. – Hello.World Mar 19 '19 at 14:25

1 Answers1

0

Use iterrows:

for index, row in df.iterrows():
    "Pre-process your data here"
    "To process particular row you can do this"
    row["column_name to process"]


Hello.World
  • 720
  • 8
  • 22