Home > OS >  How to create DataFrames with 9 and 14 elements random values comining from numbers, letters and spe
How to create DataFrames with 9 and 14 elements random values comining from numbers, letters and spe

Time:07-12

I need to create 2 DataFrame in Python Pandas:

  1. DataFame with random 9-elements values (all combinations of numbers, letters and special characters) like below ("col1" as string data type) with 100 rows:

df1

col1
-----
123456789
1234M678x
_ 3456P89
12345@78!
...
  1. DataFame with random 14-elements values (all combinations of numbers, letters and special characters) like below ("col2" as string data type) with 100 rows:

df2

col2
--------------
92345678900554
123@^678900554
12345678PPl554
8 3456$89005x4
...

CodePudding user response:

You can use string.ascii_letters , string.punctuation , string.digits.


import random
import string
import pandas as pd

ln = 9
rows = 100
df = pd.DataFrame([''.join(random.choice(string.ascii_letters   
                                         string.punctuation   
                                         string.digits) 
                           for _ in range(ln)) 
                   for _ in range(rows)],
                 columns = ['col1'])

print(df)

Output:

         col1
0   NKliCCsf8
1   BYyP`!%zN
2   35--pmn!<
3   6DRqa1ku/
4   XECuNst[b
..        ...
95  kA98&XZNk
96  ?Uo3J\3u4
97  =RHQW{I'z
98  L\R>RT|Y"
99  N'%&p{}Et

CodePudding user response:

You can use secrets

import pandas as pd

df1 = pd.DataFrame()
col1 = [secrets.token_urlsafe(9) for i in range(100)]
df1['col1'] = col1

df2 = pd.DataFrame()
col2 = [secrets.token_urlsafe(14) for i in range(100)]
df2['col2'] = col2
  • Related