pandas_series01

  1. 如何从列表,数组,字典构建series
    mylist = list('abcedfghijklmnopqrstuvwxyz')   # 列表
    myarr = np.arange(26)                          # 数组
    mydict = dict(zip(mylist, myarr))             # 字典
    
    # 构建方法
        ser1 = pd.Series(mylist)
        ser2 = pd.Series(myarr)
        ser3 = pd.Series(mydict)
        print(ser3.head())                 # 打印前5个数据
        
        #>  a    0
            b    1
            c    2
            d    4
            e    3
            dtype:int64
    1. 如何使series的索引列转化为dataframe的列
      mylist = list('abcedfghijklmnopqrstuvwxyz')
      myarr = np.arange(26)
      mydict = dict(zip(mylist, myarr))
      ser = pd.Series(mydict)
      
      # series转换为dataframe
      df = ser.to_frame()
      # 索引列转换为dataframe的列
      
      df.reset_index(inplace=True)
      print(df.head())
      
      #>	  index  0
      	0     a  0
      	1     b  1
      	2     c  2
      	3     e  3
      	4     d  4

原文地址:https://www.cnblogs.com/huaobin/p/15686935.html