美文网首页
python 积累6

python 积累6

作者: 黄yy家的jby | 来源:发表于2019-01-04 17:55 被阅读0次

    摘要

    1.对dataframe列的运算

      1. 转化成numpy计算
        但是要注意等长,可以考虑先concat成一个dataframe
    h7 = np.array(df) + np.array(df2)
    h8 = np.array(df) * np.array(df2)
    
    • 2.不能直接运算,是因为index的问题,可以考虑将index丢掉
    h5 = df.reset_index(drop = True) + df2.reset_index(drop=True)
    h6 = df.reset_index(drop=True)*df2.reset_index(drop=True)
    
      1. numpy的函数可以直接使用
    a = np.log(df.b)
    b = df.a + df2.a
    c = np.exp(df.a)
    

    最终得到的a, b, c都是series

      1. df.apply(lambda x : f(x))
      1. 多列的apply()
    df['col3'] = df.apply(lambda x: x['col1'] + 2 * x['col2'], axis=1)
    

    相关文章

      网友评论

          本文标题:python 积累6

          本文链接:https://www.haomeiwen.com/subject/bdukrqtx.html