Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'm a bit curious why you'd like it in the format. There's probably a much better way to store your data. But here goes.</p> <pre><code>In [137]: import numpy as np In [138]: import pandas as pd In [139]: df_reshape_test = pd.DataFrame( {'location' : ['A', 'A', 'A', 'B', 'B ', 'B'], 'dist_to_A' : [0, 0, 0, 50, 50, 50], 'dist_to_B' : [50, 50, 50, 0, 0, 0], 'location_var': [10, 10, 10, 14, 14, 14], 'ind_var': [3, 8, 10, 1, 3, 4]}) In [140]: print(df_reshape_test) dist_to_A dist_to_B ind_var location location_var 0 0 50 3 A 10 1 0 50 8 A 10 2 0 50 10 A 10 3 50 0 1 B 14 4 50 0 3 B 14 5 50 0 4 B 14 In [141]: # Get the new axis separately: In [142]: idx = pd.Index(df_reshape_test.index.tolist() * 2) In [143]: df2 = df_reshape_test[['ind_var', 'location', 'location_var']].reindex(idx) In [144]: print(df2) ind_var location location_var 0 3 A 10 1 8 A 10 2 10 A 10 3 1 B 14 4 3 B 14 5 4 B 14 0 3 A 10 1 8 A 10 2 10 A 10 3 1 B 14 4 3 B 14 5 4 B 14 In [145]: # Swap the location for the second half In [146]: # replace any 6 with len(df) / 2 + 1 if you have more rows.d In [147]: df2['choice'] = [1] * 6 + [0] * 6 # may need to play with this. In [148]: df2.iloc[6:].location.replace({'A': 'B', 'B': 'A'}, inplace=True) In [149]: df2 = df2.sort() In [150]: df2['dist_S'] = np.abs((df2.choice - 1) * 50) In [151]: print(df2) ind_var location location_var choice dist_S 0 3 A 10 1 0 0 3 B 10 0 50 1 8 A 10 1 0 1 8 B 10 0 50 2 10 A 10 1 0 2 10 B 10 0 50 3 1 B 14 1 0 3 1 A 14 0 50 4 3 B 14 1 0 4 3 A 14 0 50 5 4 B 14 1 0 5 4 A 14 0 50 </code></pre> <p>It's not going to generalize well, but there are probably alternative (better) ways to get around the uglier parts like generating the choice col.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload