import numpy as np import pandas as pd import tushare as ts #1. df=pd.DataFrame({"num":[1,2,3,4,3,2,1,4], "id":["A","B","C","D","C","B","A","D"]}) print(df.duplicated())#列出重复 df.drop_duplicates()#删除重复的副本 df.replace({"C":"Cred","D":"Drop"})#替换副本 #2. s=pd.Series([1,np.nan,2,np.nan]) s=s.replace({np.nan:0})#替换数据 print(s) #3. stock=ts.get_hist_data('600848') litestock=stock[["high","low"]].head(5) newline=litestock["low"].map({21.38:1,21.50:2,20.97:3,21.10:4,20.52:5}) litestock["high"]=newline df=pd.DataFrame({'item':['ball','mug','pen'], 'color':['white','rosso','verde']}) price={'ball':5.56,'mug':4.20,'pen':1.30} df["color"]=df["item"].map(price)#改变一个序列 df["color2"]=df["item"].map(price)#原来有就覆盖,原来没有就新增 #4. ddd=pd.DataFrame({'zs':[129,130,34],'ls':[136,98,8]}) def mapscore(score): if score<60: return "不及格" elif score>120: return "不错" else: return "得过且过" ddd["zsisOk"]=ddd["zs"].map(mapscore)#map作用于一维 def mapscore(score): return 1 ddd["zstimes"]=ddd["zs"].map(mapscore) print(ddd) ddd.rename({0:"one",1:"two",2:"three"})#替换索引