0

I am trying to pass constant values in a python pandas data frame, and it is adding the only first row and the rest of the show as NaN. I am not sure what I am doing wrong. I tried all the possible answers but it still not working for. If you guys have any idea please let me know

weekdayDistance.loc[:,'UID'] = user.UID.head(1)
weekdayDistance['UID'] = user.UID.head(1)
weekdayDistance.loc['UID'] = user.UID.loc[0]

output:
a b c UID
1 1 1  1
2 2 2  NaN
3 3 3  NaN

what I want 
a b c UID
1 1 1  1
2 2 2  1
3 3 3  1
vinay karagod
  • 256
  • 1
  • 3
  • 18

2 Answers2

2

Simple answer would be

df['col_name'] = 1

or you can use insert, where 0 is the index of column, followed by column name and its constant value.

df.insert(0, colname, value)

This link here might give you explanation

1

You can do like below:

user['UID'] = 1

If just one row is getting filled, you can use ffill(). It will replicate the first row's value in all the rows.

user.UID = user.UID.ffill()
Mayank Porwal
  • 33,470
  • 8
  • 37
  • 58
  • Did you try the exact statement that I gave? It should work fine. – Mayank Porwal Oct 18 '20 at 12:37
  • I am getting the value from other dataframe and passing the value to this dataframe and the above will work fine but I need the pass from other dataframe – vinay karagod Oct 18 '20 at 12:44
  • @vinaykaragod I have updated my answer. Run the `ffill()` statement after you get one row filled with constant value. This will work. Let me know if you face any issues. – Mayank Porwal Oct 18 '20 at 12:48