zoukankan      html  css  js  c++  java
  • pandas 的loc布尔索引

    在使用df的loc布尔索引时,其实里面的列可以是原来列表中没有存在的。。。

    import pandas as pd
    import numpy as np 
    
    boolean=[True,False]
    gender=['','']
    color=['green','blue','yellow']
    data=pd.DataFrame({'height':np.random.randint(150,190,100),
                       'weight':np.random.randint(40,90,100),
                       'smoker':[boolean[x] for x in np.random.randint(0,2,100)],
                       'gender':[gender[x] for x in np.random.randint(0,2,100)],
                       'age':np.random.randint(15,90,100),
                       'color':[color[x] for x in np.random.randint(0,len(color),100)]})
    data.loc[data['gender']=='','xingbie']=1 #里面的xingbie 的列原来并没有存在

    原先使用的还以为是语法错误

  • 相关阅读:
    Redis
    Redis
    Redis
    linux 安装docker
    linux 安装nexus
    linux 安装jenkins
    linux 安装gitlab
    python 类的继承
    python raise & assert
    python super()
  • 原文地址:https://www.cnblogs.com/cgmcoding/p/13905941.html
Copyright © 2011-2022 走看看