• 17-pandas处理数据的简单功能


    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"})#替换索引
    

      

  • 相关阅读:
    3.K均值算法
    2.机器学习相关数学基础
    1. 机器学习概述
    15、语法制导的语义翻译
    14、算符优先分析
    13、自下而上语法分析
    12、递归下降语法分析
    11、LL(1)文法的判断,递归下降分析程序
    10、消除左递归
    4.K均值算法--应用
  • 原文地址:https://www.cnblogs.com/wcyMiracle/p/12447714.html
Copyright © 2020-2023  润新知