Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create pandas DataFrame using numpy repeat function

I'm trying to create a pandas DataFrame in which one of the columns makes use of the numpy repeat function multiple times. Here's how I do this in R using c and rep and it works:

df <- data.frame(
  date = seq.Date(as.Date('2018-12-01'), as.Date('2019-12-01'), by='month'),
  value = c(rep(0.08, 7), rep(0.06, 6)),
)

Here's what I'm trying in pandas but it throws the error arrays must all be same length:

import numpy as np
import pandas as pd
df= pd.DataFrame({
    'date': pd.date_range('2018-12-01', '2019-12-01', freq='MS'),
    'value': [np.repeat(0.08, 7), np.repeat(0.06, 6)]
})

How can I do this in pandas?

like image 575
Gaurav Bansal Avatar asked Dec 24 '22 08:12

Gaurav Bansal


1 Answers

np.repeat can take an array-like thing for the repeats argument.

df = pd.DataFrame({
    'date': pd.date_range('2018-12-01', '2019-12-01', freq='MS'),
    'value': np.repeat([.08, .06], [7, 6])
})
like image 195
piRSquared Avatar answered Jan 05 '23 17:01

piRSquared