I would like to create a data frame with several columns with different lengths, as I think this is not possible with pd.dataframe I create first a dataframe just with zeros and now I would like to replace each column for an array that I have stored before (with different lengths). I have tried dataframe.replace and dataframe.update but I couldn't get this results.
2 Answers
You have to insert the array from index 1. For that you can do,
df['dobs'][1:] = dobs
Similarly for all the arrays.
Consider a sample dataframe,
df = pd.DataFrame()
df['dobs'] = [0.] * 45
df['dpred_0'] = [0.] * 45
df['dpred'] = [0.] * 45
df['mrec'] = [0.] * 45
Now, some place holders arrays of the shape you mentioned in the question,
dobs = np.array([x for x in range(1, 45)])
dpred_0 = np.array([x for x in range(1, 45)])
dpred = np.array([x for x in range(1, 45)])
mrec = np.array([x for x in range(1, 46)])
Let's check the shapes,
print(dobs.shape, dpred_0.shape, dpred.shape, mrec.shape, df.shape) # ((44,), (44,), (44,), (45,), (45, 4))
To replace the columns from index 1 for shorter arrays you can do like this,
df['dobs'][1:] = dobs
df['dpred_0'][1:] = dpred_0
df['dpred'][1:] = dpred
df['mrec'] = mrec # mrec is of shape (45, ) so no need to start from index 1
dobs dpred_0 dpred mrec
0 0.0 0.0 0.0 1
1 1.0 1.0 1.0 2
2 2.0 2.0 2.0 3
3 3.0 3.0 3.0 4
4 4.0 4.0 4.0 5

