0

I am having a problem with replacing a specific column using its index in a dataframe with a new dataframe that consists of only 1 column given that they both have the same length

I need to replace the column only knowing its index as I am choosing a random column to replace in the dataframe df that contains 8 columns with the new dataframe df_temp that only has 1 column

N=random.randint(1,8)
df.iloc(: , [N - 1]) = df_temp.values

This gives me syntax error I don't know if I am using the .iloc wrong or there is an alternative way to do that.

moham
  • 41
  • 3

1 Answers1

0

I am not clearly understand, but can you try it:

df.iloc[:, [N-1]] = df_temp.values
claymorehack
  • 78
  • 10
  • its giving me this error 'tuple' object has no attribute 'values' what I am trying to do is to replace the column which has index N-1 in df with the only column in df_temp as it has one column and both are equal in length – moham Nov 15 '20 at 10:14
  • To verifying your explanation, for example df has 5 columns, and you want to copy df's 4th of columns to df_temp, is this what you want? – claymorehack Nov 15 '20 at 11:21
  • No I want to replace df's 4th column with example with the column in df_temp – moham Nov 15 '20 at 12:13
  • Ok i found this post maybe it's helpful [https://stackoverflow.com/questions/36413993/replace-column-values-in-one-dataframe-by-values-of-another-dataframe](https://stackoverflow.com/questions/36413993/replace-column-values-in-one-dataframe-by-values-of-another-dataframe) – claymorehack Nov 15 '20 at 13:51