Home > Software engineering >  How to create DataFrame with 100 values consisting of 10 elementary numbers in Python Pandas?
How to create DataFrame with 100 values consisting of 10 elementary numbers in Python Pandas?

Time:07-11

I need to create Data Frame in Python Pandas with 100 rows with random values consisting of 10 elementary numbers. So as a result I need something like below "col1" has to be as date type string:

col1
---------
1233459857
8463746781
9084756289
...

How can I do that in python pandas ?

CodePudding user response:

To ensure that values beginning with one or more zeros are properly formatted, we can create values of data type string with zero padding to 10 places:

rng = np.random.default_rng()
df = pd.DataFrame(pd.Series(rng.integers(0, 10**10, size=100)).apply(lambda n: f'{n:0>10}'), columns=['col1'])

Output:

          col1
0   9448137321
1   9080789825
2   0131300041
3   6795692128
4   5236197395
..         ...
95  0541351442
96  6920748551
97  9563382744
98  2385042247
99  3736754262

CodePudding user response:

Use np.random.Generator.integers to create the integers:

df = pd.DataFrame({"col1": np.random.default_rng().integers(1000000000, 10000000000, 100)})
print(df)

col1
0   7612431035
1   4173337084
2   5208961112
3   4717726170
4   8300616999
..         ...
95  4685852377
96  2047960728
97  2354643233
98  6465408928
99  7202650975

[100 rows x 1 columns]
  • Related