Pandas idiomatic way to look up a dictionary


pythonic metaphor

I have a series of pandas integers (they are limited to some small finite subset) and a dictionary that doubles these possible integers. I want to create a new series that looks like dictionary[series]. What is the pandas idiomatic way?

Alex Reilly

You can use to do this.map

For example, here's a dictionary that maps several integers nto n + 0.5, and a sequence of integers:

>>> d = {1: 1.5, 2: 2.5, 3: 3.5, 4: 4.5, 5: 5.5}
>>> s = pd.Series([1, 2, 3, 4, 5]
>>> s
0    1
1    2
2    3
3    4
4    5
dtype: int64

To create a new series you can write:

>>> s.map(d)
0    1.5
1    2.5
2    3.5
3    4.5
4    5.5
dtype: float64

In addition to a dictionary, mapit can also accept another Series as a parameter or function.

Related


Pandas idiomatic way to customize fillna

Kyiv I have time series data in the following format, where one value represents the cumulative amount since the last recording. What I want to do is to "scatter" the accumulators that contain NaNs in the past so that this input: s = pd.Series([0, 0, np.nan, n

key to look up value in integer dictionary

Jaycechan The problem is to write a Python function that returns a list of keys in aDict with a value target. All keys and values in the dictionary are integers, and the keys in the list we return must be in ascending order. Here is what I have so far: def key

Look up order in my dictionary using customer name

direct current I need to look up an order in a dictionary using the name, but can't figure out how to use the name (a string) to find a matching order since the value of the dictionary is the object order. Public Class MainForm Public Shared orders As New

How to look up a dictionary in one list but not in another

quitford Suppose we have a list of 2 dictionaries L1 and L2. I want a list of dictionaries that are in L2 but not in L1. In my case, I have L1 that is a subset of L2, so I'm not sure if this fact can be used to make any optimizations. figure point solution [_d

Pandas idiomatic way to customize fillna

Kyiv I have time series data in the following format, where one value represents the cumulative amount since the last recording. What I want to do is to "scatter" the accumulators that contain NaNs in the past so that this input: s = pd.Series([0, 0, np.nan, n

Pandas idiomatic way to customize fillna

Kyiv I have time series data in the following format, where one value represents the cumulative amount since the last recording. What I want to do is to "scatter" the accumulators that contain NaNs in the past so that this input: s = pd.Series([0, 0, np.nan, n

Pandas idiomatic way to customize fillna

Kyiv I have time series data in the following format, where one value represents the cumulative amount since the last recording. What I want to do is to "scatter" the accumulators that contain NaNs in the past so that this input: s = pd.Series([0, 0, np.nan, n

How to look up word in macos dictionary using python subprocess

ntf I'm writing a script to open and search the macOS Dictionary app In terminal, I can do this open dict://cheeseburger The app will then open the "Cheeseburger" entry Using python's subprocess module, I can do this: subprocess.Popen(["path_to_dictionary_app"

How to look up word in macos dictionary using python subprocess

ntf I'm writing a script to open and search the macOS Dictionary app In terminal, I can do this open dict://cheeseburger The app will then open the "Cheeseburger" entry Using python's subprocess module, I can do this: subprocess.Popen(["path_to_dictionary_app"

How to look up an instance of a class in a dictionary?

Ben If I've been doing proper research on this, I've had some help before and a user said it would be nice to use a Dictionaryto store my Countrysum Places. So I created Dictionary: Dictionary<string, NewCountryClass> NTCD = new Dictionary<string, NewcountryCl

How often to look up dictionary keys from a list of dictionaries

Saranya Gupta I should find out the frequency of distinct keys in a list of dictionaries. E.g: Enter a list of dictionaries: [{'p1': 'val1', 'p2': 'val2', 'p3': 'val3', 'p4': 'val4'}, {'p1': 'val5', 'p7': 'val6', 'p3': 'val7'}, {'p1': 'val8', 'p2': 'val9', '

Pandas idiomatic way to look up a dictionary

pythonic metaphor I have a series of pandas integers (they are limited to some small finite subset) and a dictionary that doubles these possible integers. I want to create a new series that looks like dictionary[series]. What is the pandas idiomatic way? Alex

Pandas idiomatic way to customize fillna

Kyiv I have time series data in the following format, where one value represents the cumulative amount since the last recording. What I want to do is to "scatter" the accumulators that contain NaNs in the past so that this input: s = pd.Series([0, 0, np.nan, n

Pandas idiomatic way to customize fillna

Kyiv I have time series data in the following format, where one value represents the cumulative amount since the last recording. What I want to do is to "scatter" the accumulators that contain NaNs in the past so that this input: s = pd.Series([0, 0, np.nan, n