1
   df  


    ts_code     close     ma50   ma200  ma50_to_close   ma200_to_close
0   000902.SZ   12.20   11.9982 9.5290  1.016819       1.280302
4   000902.SZ   11.94   12.0378 9.4423  0.991876       1.264522
0   000799.SZ   100.39  88.6272 55.8328 1.132722       1.798047
4   000799.SZ   101.33  85.9644 54.5181 1.178744       1.858649

Expected dataframe

column names:

ts_code  curr_close  curr_ma50   curr_ma200  curr_ma50_to_close   curr_ma200_to_close  l5d_close  l5d_ma50   l5d_ma200  l5d_ma50_to_close   l5d_ma200_to_close

data

0 000902.SZ 12.20   11.9982 9.5290  1.016819 1.280302 11.94 12.0378 9.4423  0.991876       1.264522
1 000799.SZ 100.39  88.6272 55.8328 1.132722 1.798047 101.33 85.9644 54.5181 1.178744      1.858649

Because it's hard to show expected dataframe. I show column names and data separately.

Try and refer

I refer this post. It's similar but the data structure is not the same.

Jack
  • 1,724
  • 4
  • 18
  • 33
  • 1
    Please try Q/A 10 and 11 in the duplicate link. If it's not working out for you, please state why (in the question). – Quang Hoang Feb 13 '21 at 02:05
  • @QuangHoang Q/A 10 and 11 it's not similar. For Q10, the duplicated values in columns will be the new column name of dataframe but mine is that the duplicated values is still as column values not as columns names. For Q11, it's multi-index problem with column names but mine is that the column names should not be flatten. – Jack Feb 13 '21 at 02:43
  • Once you creat the count column, `df.pivot(index='ts_code', columns='count')'. Then you have a multi-index and it becomes Q11. – Quang Hoang Feb 13 '21 at 03:00

0 Answers0