0

I have a dataset where some rows are split over many columns more than they should. I am trying to shift some rows manually using the following code:

df_train.iloc[3537]["title"] = df_train.iloc[3537]["title"] + "" + df_train.iloc[3537]["text"]

df_train.iloc[3537]["text"] = df_train.iloc[3537]["label"] + "" + df_train.iloc[3537]["X1"]

df_train.iloc[3537]["label"] = df_train.iloc[3537]["X2"]

So basically Im just accessing the row by index and replacing the content of the column with what I need. However it doesnt seem to work when I try accessing the rows again, they remain unchanged.

Sid
  • 3,749
  • 7
  • 29
  • 62
principe
  • 173
  • 1
  • 1
  • 11
  • The problem is likely chained assignment. `][`. If it's a rangeIndex, I'd switch to `.loc[3537, 'title']` – ALollz May 09 '19 at 15:43
  • @anky_91 it is all text data that i am shifting from one column to the one next to it – principe May 09 '19 at 15:46
  • Possible duplicate of [Combine two columns of text in dataframe in pandas/python](https://stackoverflow.com/questions/19377969/combine-two-columns-of-text-in-dataframe-in-pandas-python) – Sid May 09 '19 at 15:48

1 Answers1

0

I think you should change the iloc to loc and also change syntax a bit. Use df_train.loc[3537, "title"] insead of df_train.iloc[3537]["title"] at every place. Thats likely to work.

  • Almost, but `.iloc` cannot mix integer and label based indices. You would need: `df.iloc[3537, df.columns.get_loc('title')]` – ALollz May 09 '19 at 15:47