3

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.

enter image description here

The type and shapes of the arrays are: enter image description here

0

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

Comments

1

You can add an array to a dataframe by creating a new column and define the length and position:

random_array = range(0,12)
df['new_column'][0:12] = random_array

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.