Relative Content

Tag Archive for pythonpandaslistdictionary

Convert a dataframe with lists to a dictionary

I have a dataframe that contains a identifier and list in each cell. The idea is to create a dictionary in which the keys are the identifiers and the values are a list with the elements that appears in the dataframe. Identifiers may repeat with different lists so they must be combined for the dictionary. Eg.

python dict is giving key error even if initialized with some values

data={} vega={} for coin in [‘BTC’]: vega[coin] = {} data[coin] = {} data[coin][‘columns’]=[’27-Apr-24′, ’28-Apr-24′, ’29-Apr-24′, ’03-May-24′, ’10-May-24′, ’17-May-24′, ’31-May-24′] for expiry in data[coin][‘columns’]: vega[coin][expiry] = data[coin].get(‘Vega’, {}).get(‘Total’, {}).get(expiry, 0) for coin in [‘BTC’]: data[coin][‘columns’]=[’27-Apr-24′, ’28-Apr-24′, ’29-Apr-24′, ’03-May-24′, ’10-May-24′, ’17-May-24′, ’31-May-24′, ’28-Jun-24′] for expiry in data[coin][‘columns’]: vega[coin][expiry] += data[coin].get(‘Vega’, {}).get(‘Total’, {}).get(expiry, 0) Im initializing dict, and […]