Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to create DataFrames with 9 and 14 elements random values comining from numbers, letters and special characters in Python Pandas?

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading