Mutliply два фрейма данных по указанному индексу - PullRequest
1 голос
/ 05 августа 2020

Я немного потерялся в этом. Любая помощь приветствуется.

Первый фрейм данных:

            2020-08-03    2020-08-04
currency                            
1WO       8.255000e+00  8.137000e+00
ADH       6.349000e-02  6.388000e-02
...                ...           ...

XRP       3.292000e+01  3.184000e+01

[130 rows x 2 columns]

второй фрейм данных:

                          2020-08-03    2020-08-04
app_id      currency                            
45334         1WO       2.614163e+05  2.614163e+05
              ADH       8.403654e+05  8.403654e+05
...                              ...           ...
23423         FRT       1.078614e+03  1.057127e+03
              WES       7.844820e+06  7.936699e+06

[148 rows x 2 columns]

Код:

jpy_bal = first_df.multiply(second_df, axis= 0, level='currency').fillna(0).astype(float)

Ошибка :

   "Index._join_level on non-unique index " "is not implemented"
NotImplementedError: Index._join_level on non-unique index is not implemented

1 Ответ

0 голосов
/ 05 августа 2020

Проблема с дублированием в first_df.index:

#simulate error
print (first_df)
          2020-08-03  2020-08-04
currency                        
1WO          8.25500     8.13700
1WO          0.06349     0.06388
XRP         32.92000    31.84000

jpy_bal = first_df.multiply(second_df, axis= 0, level='currency').fillna(0).astype(float)
print (jpy_bal)
NotImplementedError: Index._join_level on non-unique index is not implemented

Возможное решение - агрегирование по sum или mean по индексу:

first_df = first_df.groupby(level=0).sum()
print (first_df)
          2020-08-03  2020-08-04
currency                        
1WO          8.31849     8.20088
XRP         32.92000    31.84000

jpy_bal = first_df.multiply(second_df, axis= 0, level='currency').fillna(0).astype(float)
print (jpy_bal)
                   2020-08-03    2020-08-04
app_id currency                            
45334  1WO       2.174589e+06  2.143844e+06
       ADH       0.000000e+00  0.000000e+00
23423  FRT       0.000000e+00  0.000000e+00
       WES       0.000000e+00  0.000000e+00

Или удалить дубликаты по индексу:

first_df = first_df[~first_df.index.duplicated()]
print (first_df)
          2020-08-03  2020-08-04
currency                        
1WO            8.255       8.137
XRP           32.920      31.840

jpy_bal = first_df.multiply(second_df, axis= 0, level='currency').fillna(0).astype(float)
print (jpy_bal)
                   2020-08-03    2020-08-04
app_id currency                            
45334  1WO       2.157992e+06  2.127144e+06
       ADH       0.000000e+00  0.000000e+00
23423  FRT       0.000000e+00  0.000000e+00
       WES       0.000000e+00  0.000000e+00
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...