Как объединить разные значения с разных листов в один?Python

Программы на Python
Ответить
Anonymous
 Как объединить разные значения с разных листов в один?

Сообщение Anonymous »

Я пытаюсь добавить «Ставку» в новый столбец в fills_df. fills_df имеет вкладку «Ликвидность», которая соответствует коду Liq в liq_rate_df, с которым связана ставка. Я хочу добавить ставки в fills_df, используя код Liq из liq_rate_df, который соответствует ликвидности в fills_df.

Код: Выделить всё

import pandas as pd
liq_rate_df = pd.read_excel(r'/content/gdrive/My Drive/fills_today_mh - 2023-2024 (1).xlsx', sheet_name='liq_rate')
fills_df = pd.read_excel(r'/content/gdrive/My Drive/fills_today_mh - 2023-2024 (1).xlsx', sheet_name='fills')

# Create a dictionary mapping of liquidity codes to rates
rate_mapping = dict(zip(liq_rate_df['Liq Code'], liq_rate_df['Rate']))

# Create a new column 'rate' in fills_df by mapping the 'liquidity_code' in fills_df to rate_mapping
fills_df['rate'] = fills_df['Liq Code'].map(rate_mapping)
Ошибка:

Код: Выделить всё

KeyError                                  Traceback (most recent call last)
/usr/local/lib/python3.10/dist-packages/pandas/core/indexes/base.py in get_loc(self, key)
3790         try:
-> 3791             return self._engine.get_loc(casted_key)
3792         except KeyError as err:

index.pyx in pandas._libs.index.IndexEngine.get_loc()

index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'Liq Code'

The above exception was the direct cause of the following exception:

KeyError                                  Traceback (most recent call last)
2 frames
/usr/local/lib/python3.10/dist-packages/pandas/core/indexes/base.py in get_loc(self, key)
3796             ):
3797                 raise InvalidIndexError(key)
-> 3798             raise KeyError(key) from err
3799         except TypeError:
3800             # If we have a listlike key, _check_indexing_error will raise

KeyError: 'Liq Code'
liq_rate_df.head()
Тип ставки по коду Liq
0 A -0,0001 на акцию
1 B -0,0001 Условные
2 C 0,0010 на акцию
3 D 0,0002 на акцию

Подробнее здесь: https://stackoverflow.com/questions/790 ... s-into-one
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Python»