私信  •  关注

Winand DSM

Winand DSM 最近创建的主题
Winand DSM 最近回复了
5 年前
回复了 Winand DSM 创建的主题 » 如何在pandas python中为字符串创建汇总列[duplicate]

你可以使用 .replace 。例如:

>>> 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

或者直接在 Series ,即 df["col1"].replace(di, inplace=True) .