Home > Mobile >  How to add more rows of random values to an existing column in my dataset - pandas?
How to add more rows of random values to an existing column in my dataset - pandas?

Time:11-05

I want to add ten more rows to each column of the dataset provided below. It should add random integer values ranging from :

  1. 20-27 for temperature
  2. 40-55 for humidity
  3. 150-170 for moisture

Dataset: Temperature Humidity Moisture

      0 22        46      0 
      1 36        41.4    170 
      2 18        69.3    120 
      3 21        39.3    200 
      4 39        70      150 
      5 22        78      220 
      6 27        65      180 
      7 32        75      250

I have tried: import numpy as np import pandas as pd

data1 = np.random.randint(20,27,size=10) df = pd.DataFrame(data, columns=['Temperature'])

print(df)

This method deletes all the existing row values and gives out only the random values. What I all need is the existing rows and the random values in addition.

CodePudding user response:

Use:

df1 = pd.DataFrame({'Temperature':np.random.randint(20,28,size=10),
                   'Humidity':np.random.randint(40,56,size=10),
                   'Moisture':np.random.randint(150,171,size=10)})

df = pd.concat([df, df1], ignore_index=True)

print (df)
    Temperature  Humidity  Moisture
0            22      46.0         0
1            36      41.4       170
2            18      69.3       120
3            21      39.3       200
4            39      70.0       150
5            22      78.0       220
6            27      65.0       180
7            32      75.0       250
8            20      52.0       158
9            21      45.0       156
10           23      49.0       151
11           24      51.0       167
12           22      45.0       157
13           21      43.0       163
14           26      55.0       162
15           25      40.0       164
16           24      40.0       155
17           20      48.0       150
  • Related