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?
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])
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With