简体   繁体   English

如何更新 Pandas 中的 DataFrame 列?

[英]How to update DataFrame column in Pandas?

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 .我想将Col1的值更新为ord(Col1) - ord('A') + 1 ,忽略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.请注意,在 Pandas 中,大多数操作都是通过内部数据对齐完成的。 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.这意味着熊猫通常会尝试匹配索引,因此在此处删除 NaN 值将产生 NaN 值,其中索引不在左侧。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM