samedi 7 décembre 2019

Pandas to create dataframe of mix of integers and floats in given ranges

A list of 5 ranges, that I want to create a dataframe (10 rows * 5 columns) from. The values of the columns will be random numbers in the given range.

The given ranges are a mix of integers and floats, i.e. [1,31] is of integer, [4, 172.583333] is of floats etc.

Below works for outputs of either integers or floats only.

How can I have an output of a mix of integers and floats together? i.e. column A is random of integers, column B is floats, column C is floats, D and E are integers.

Thank you.

import numpy as np
import pandas as pd

min_max = [
[1, 31],
[4, 172.583333],
[0, 88.50561],
[4, 297],
[3, 37]]

for a, b in min_max:
    df = pd.DataFrame(np.random.randint(a,b,size=(10, 5)), columns=list('ABCDE'))   # to generate intergers only
    df = pd.DataFrame(np.random.uniform(a,b,size=(10, 5)), columns=list('ABCDE'))   # to generate floats only



Aucun commentaire:

Enregistrer un commentaire