'How to use Python faker for dependent columns

Scenario If column1 = ‘Value’ then column2 = ‘AAA’

How can we use faker to generate mock data for these dependent columns. Need to consider both positive and negative data.



Solution 1:[1]

You can use Faker database like this:

import pandas as pd
from faker.providers import date_time

df = (pd.DataFrame(date_time.Provider.countries, columns=['name', 'alpha-2-code'])
        .rename(columns={'name': 'country', 'alpha-2-code': 'country_code'})
        .sample(n=1000, replace=True, ignore_index=True, random_state=2022))

Output:

>>> df
                              country country_code
0                              Rwanda           RW
1                             Grenada           GD
2                                Oman           OM
3                             Moldova           MD
4    Saint Vincent and the Grenadines           VC
..                                ...          ...
995                           Iceland           IS
996                        Seychelles           SC
997                            Israel           IL
998                 Equatorial Guinea           GQ
999               Republic of Ireland           IE

[1000 rows x 2 columns]

Or use pycountry.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Corralien