python笔记:3.2.2.3pandas数据操作

    xiaoxiao2022-07-12  161

    # -*- coding: utf-8 -*- """ Created on Thu May 23 15:37:10 2019 @author: User """ import pandas as pd import numpy as np cs1=pd.Series([1.5,2.5,3,5,1],index=['a','c','d','b','e']) print("\n cs1:") print(cs1) cs2=pd.Series([10,20,30,50,10,100,20],index=['c','a','e','b','f','g','d']) print("\n cs2:") print(cs2) print("\n cs1 + cs2:") print(cs1 + cs2) cdf1=pd.DataFrame(np.arange(10).reshape((2,5)),columns=list('bcaed')) print("\n cdf1:") print(cdf1) cdf2=pd.DataFrame(np.arange(12).reshape((3,4)),columns=list('abcd')) print("\n cdf2:") print(cdf2) print("\n cdf1 + cdf2:") print(cdf1 + cdf2) print("\n cdf1.add(cdf2,fill_value=0):") print(cdf1.add(cdf2,fill_value=0))

    运行:

     cs1: a    1.5 c    2.5 d    3.0 b    5.0 e    1.0 dtype: float64

     cs2: c     10 a     20 e     30 b     50 f     10 g    100 d     20 dtype: int64

     cs1 + cs2: a    21.5 b    55.0 c    12.5 d    23.0 e    31.0 f     NaN g     NaN dtype: float64

     cdf1:    b  c  a  e  d 0  0  1  2  3  4 1  5  6  7  8  9

     cdf2:    a  b   c   d 0  0  1   2   3 1  4  5   6   7 2  8  9  10  11

     cdf1 + cdf2:       a     b     c     d   e 0   2.0   1.0   3.0   7.0 NaN 1  11.0  10.0  12.0  16.0 NaN 2   NaN   NaN   NaN   NaN NaN

     cdf1.add(cdf2,fill_value=0):       a     b     c     d    e 0   2.0   1.0   3.0   7.0  3.0 1  11.0  10.0  12.0  16.0  8.0 2   8.0   9.0  10.0  11.0  NaN  

    最新回复(0)