mariancatholic
mariancatholic

Reputation: 91

Sample a vector from a dataframe and reference into another dataframe in pandas

I am stuck in something I want to do.

I have a dataframe that consists of 1180 rows x 24 columns but of course for simplification I'll just put this example.

df1=

    1      2      3
1  **0.21  0.45    0.67**
2  0.28  0.98    0.87
3  **0.56  0.67    0.98**
4  0.87  0.86    0.76

df2=

1/1/2022 01:00
1/1/2022 02:00
1/1/2022 03:00

2/1/2022 01:00
2/1/2022 02:00
2/1/2022 03:00

What I am trying to achieve is this:

1/1/2022 01:00  **0.21**  
1/1/2022 02:00  **0.45**    
1/1/2022 03:00  **0.67**

2/1/2022 01:00  **0.56**
2/1/2022 02:00  **0.67**
2/1/2022 03:00  **0.98**

I am trying to sample different row vectors and copy them transposed from 1st dataframe into 2nd dataframe. Let's say I sample 4 times. Then the vectors in the second dataframe should change everytime. In this example it was vectors 1 and 3 that where sampled in the second dataframe.

I know how to sample for example from one dataframe but not from 1 to another and much less sample rows in transposed vectors.

Would really appreciate some help/direction/ideas on how to do that.

Many thanks

Upvotes: 0

Views: 152

Answers (1)

Quang Hoang
Quang Hoang

Reputation: 150735

Try flatten the underlying numpy array:

# number of days in df2
num_days = 2

samples = df1.sample(n=numdays)

# assign to df2
df2['value'] = samples.to_numpy().ravel()

Upvotes: 1

Related Questions