Adding a New Pandas Column With Mapped Value from a Dictionary

pandas - add new column to dataframe from dictionary

Call map and pass the dict, this will perform a lookup and return the associated value for that key:

In [248]:

d = {112: 'en', 113: 'es', 114: 'es', 111: 'en'}
df['D'] = df['U'].map(d)
df
Out[248]:
U L D
0 111 en en
1 112 en en
2 112 es en
3 113 es es
4 113 ja es
5 113 zh es
6 114 es es

map value of nested dictionary to new column in dataframe in python

Looks like a list comprehension with a double loop should do the job:

df['malicious'] = [d.get('malicious') for outer_d in df['dictionary'] for d in outer_d.values()]

or you can try the following code that creates a DataFrame out of the "dictionary" column and gets the value under the "malicious" key using str.get:

df['malicious'] = (pd.DataFrame(df['dictionary'].tolist()).set_axis([0]*len(df), axis=1)
.groupby(level=0, axis=1)
.first()[0]
.str.get('malicious'))

Output:

                                          dictionary  malicious
0 {'https://twitter.com/tonythehuff/status/92718... False
1 {'http://giveaway.amazon.com\__CONNECTIONPOOL_... False
2 {'http://pcktpro.com\__CONNECTIONPOOL_ERROR__'... True

Pandas Add column based on dict value of another column

This should do the trick, use map:

your_dict={
"Val1" : "new_val1",
"Val2" : "new_val2",
"Val3" : "new_val3",
}

df['col_new'] = df['col1'].map(your_dict)

which prints your desired output:

   col1   col_new
0 Val1 new_val1
1 Val2 new_val2
2 Val3 new_val3
3 Val1 new_val1
4 Val1 new_val1
5 Val1 new_val1

Remap values in pandas column with a dict, preserve NaNs

You can use .replace. For example:

>>> df = pd.DataFrame({'col2': {0: 'a', 1: 2, 2: np.nan}, 'col1': {0: 'w', 1: 1, 2: 2}})
>>> di = {1: "A", 2: "B"}
>>> df
col1 col2
0 w a
1 1 2
2 2 NaN
>>> df.replace({"col1": di})
col1 col2
0 w a
1 A 2
2 B NaN

or directly on the Series, i.e. df["col1"].replace(di, inplace=True).

Map dict keys to new column in pandas df

Use DataFrame.drop_duplicates:

df = df.drop_duplicates('Time')

If possible, here is another solution - it match values by Series.map:

#swap key values in dict
#http://stackoverflow.com/a/31674731/2901002
d = {k: oldk for oldk, oldv in hdict.items() for k in oldv}
df["H"] = df['Code'].map(d)
df = df.dropna(subset=['H']).drop_duplicates('Time')
print (df)
Time Code H
0 2019-08-02 09:50:10.1 A 1
2 2019-08-02 09:50:10.2 X 2
3 2019-08-02 09:50:10.3 Y 2
4 2019-08-02 09:50:10.4 A 1
6 2019-08-02 09:50:10.5 X 2
8 2019-08-02 09:50:10.6 Z 2

How to add a column with values of a dictionary in Python

Is this what you want?

df = df.set_index('id')
dictionary = {1:[5,8,6,3], 2:[1,2], 5:[8,6,2]}
df['new_column'] = pd.Series(dictionary)

Note: The keys of the dictionary need to be the same type (int) as the index of the data frame.

>>> print(df)
gender new_column
id
1 0 [5, 8, 6, 3]
2 0 [1, 2]
3 1 NaN
4 1 NaN
5 1 [8, 6, 2]

Update:

A better solution if 'id' column contains duplicates (see comments below):

df['new_column'] = df['id'].map(dictionary)


Related Topics



Leave a reply



Submit