1

I need to create a dataframe with a given number of rows, say for instance it is n and with a unique value stored in a variable, for instance, unique_value.

Given n = 6 and unique_value = 25, the expected output would be a dataframe with a single column, 6 rows and in all of them, a 25:

25
25
25
25
25
25
0

2 Answers 2

5

Like this?

import pandas as pd

n = 6
unique_value = 25
df = pd.DataFrame([unique_value] * n)

#print(df)
#    0
#0  25
#1  25
#2  25
#3  25
#4  25
#5  25
Sign up to request clarification or add additional context in comments.

Comments

2

Another way is using : np.ones() which:

Returns a new array of given shape and type, filled with ones.

df=pd.DataFrame(np.ones(n)*unique_value)
print(df)

      0
0  25.0
1  25.0
2  25.0
3  25.0
4  25.0
5  25.0

Comments

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.