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

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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()

3 Comments

Did you try the exact statement that I gave? It should work fine.
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
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.