I have a dataframe
Col1 Col2
A ...
C ...
B ...
X ...
NaN ...
D ...
I want to update the values in Col1
to ord(Col1) - ord('A') + 1
, ignoring NaN
. How to update it?
Try:
df['Col1'] = df['Col1'].dropna().map(ord) - 66
Output:
Col1 Col2
0 -1.0 ...
1 1.0 ...
2 0.0 ...
3 22.0 ...
4 NaN ...
5 2.0 ...
Note in pandas, most operations are done with intrinsic data alignment. Meaning that pandas will mostly always try to match indexes, hence dropping NaN values here will produce NaN values where the index isn't present on the left-side.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.