2024-04-23 08:56:57 发布
网友
我有一个数据帧为
data = pd.DataFrame({ 'A': [1,1,1,-1,1,1], 'B':['abc','def','ghi','jkl','mno','pqr'] }) data['A'].replace(1,2)
回报
0 2 1 2 2 2 3 -1 4 2 5 2
但为什么data['A'].replace(1,"pos")不起作用?
data['A'].replace(1,"pos")
您也可以尝试使用“map”函数。
map_dict = {1: "pos"} data["test"] = data["A"].map(map_dict) data ----------------------- | | A | B | test | ----------------------- | 0 | 1 | abc | pos | | 1 | 1 | def | pos | | 2 | 1 | ghi | pos | | 3 | -1 | jkl | NaN | | 4 | 1 | mno | pos | | 5 | 1 | pqr | pos | -----------------------
阅读更多:http://pandas.pydata.org/pandas-docs/version/0.17.1/generated/pandas.Series.map.html
您需要将列“A”转换为类型字符串。
data['A'] = data['A'].astype(str)
然后再试试
data['A'].replace(str(1),'s')
您也可以尝试使用“map”函数。
阅读更多:http://pandas.pydata.org/pandas-docs/version/0.17.1/generated/pandas.Series.map.html
您需要将列“A”转换为类型字符串。
然后再试试
相关问题 更多 >
编程相关推荐