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)
.
Remap values in a Pandas column based on dictionary key/value pairs using RegEx in replace() function
One approach would be using columns
attribute:
regex_patterns = {
'last_name' : '[^A-Za-z \/\-\.\']',
'first_name' : '[^A-Za-z \/\-\.\']',
'salary' : '[^0-9 ]'
}
for column in df.columns:
df[column] = df[[column]].replace(regex_pattern[column], np.NaN, regex=True)
Replace Pandas DataFrame column values based on containing dictionary keys
You could use DataFrame.replace
with regex
parameter set to True and pass the mapping dictionary.
df.replace(dictionary, regex=True)
# col2
# 0 5
# 1 abc
# 2 8
This usage of df.replace
is less known. You can read more about it here.
Replace values in columns of dataframe based on dictionary not working
Try this method based on your example.
df1['A'] = df1['A'].map(newVals)
Remap values in pandas using a dict produces one column series instead of the full DataFrame
I think what you are trying to do is this:
data['last'] = data['last'].map(my_dict)
Updating based on comment with relation to the link:
In [1]: di = {1: "A", 2: "B"}
In [5]: from numpy import NaN
In [6]: df = DataFrame({'col1':['w', 1, 2], 'col2': ['a', 2, NaN]})
In [7]: df
Out[7]:
col1 col2
0 w a
1 1 2
2 2 NaN
In [8]: df['col1'].map(di)
Out[8]:
0 NaN
1 A
2 B
Name: col1, dtype: object
In [9]: df
Out[9]:
col1 col2
0 w a
1 1 2
2 2 NaN
In [10]: df['col1'] = df['col1'].map(di)
In [11]: df
Out[11]:
col1 col2
0 NaN a
1 A 2
2 B NaN
If you want this to happen in data3
instead of data
then you could assign the Series result of the map to a column in data3
.
pandas: replace column value with keys and values in a dictionary of list values
The best is to change the logic and try to minimize the pandas steps.
You can craft a dictionary that will directly contain your ideal output:
dic2 = {v:k for k,l in dic.items() for v in l}
# {'can': 'Should', 'could': 'Should', 'shall': 'Could', 'will': 'Would'}
# or if not yet formatted:
# dic2 = {v.lower():k.capitalize() for k,l in dic.items() for v in l}
import re
regex = '|'.join(map(re.escape, dic2))
df['text'] = df['text'].str.replace(f'\b({regex})\b',
lambda m: dic2.get(m.group()),
case=False, # only if case doesn't matter
regex=True)
output (as text2 column for clarity):
text text2
0 can you open the door? Should you open the door?
1 shall you write the address? Could you write the address?
Remapping values in a Pandas column when the column is a list or set
If want replace if match values in lists use get
in list comprehension with filter list by isinstance
:
f = lambda x: [di.get(y,y) for y in x] if isinstance(x, list) else x
df['col1'] = df['col1'].apply(f)
print (df)
col1 col2
0 w a
1 [A, B] 2
2 [B, B] NaN
There are some another solutions whats happens if no match, added 3
to list:
print (df)
col1 col2
0 w a
1 [1, 2] 2
2 [2, 2, 3] NaN
#if no match return original, here 3
f1 = lambda x: [di.get(y,y) for y in x] if isinstance(x, list) else x
df['col11'] = df['col1'].apply(f1)
#if no match return None
f2 = lambda x: [di.get(y,None) for y in x] if isinstance(x, list) else x
df['col12'] = df['col1'].apply(f2)
#if no match remove not match value
f3 = lambda x: [di[y] for y in x if y in di] if isinstance(x, list) else x
df['col13'] = df['col1'].apply(f3)
print (df)
col1 col2 col11 col12 col13
0 w a w w w
1 [1, 2] 2 [A, B] [A, B] [A, B]
2 [2, 2, 3] NaN [B, B, 3] [B, B, None] [B, B]
Related Topics
Why Is Reading Lines from Stdin Much Slower in C++ Than Python
Decode HTML Entities in Python String
How to Select a HTML Element No Matter What Frame It Is in in Selenium
Render HTML to Pdf in Django Site
Checking If Element Exists With Python Selenium
How to Get Text of an Element in Selenium Webdriver, Without Including Child Element Text
Beautiful Soup Findall Doesn't Find Them All
Staleelementreferenceexception on Python Selenium
Convert Rgb Color to English Color Name, Like 'Green' With Python
How to Use 'Subprocess' Command With Pipes
How to Get Linux Console Window Width in Python
Two Versions of Python on Linux. How to Make 2.7 the Default
Using Pip3: Module "Importlib._Bootstrap" Has No Attribute "Sourcefileloader"