pandas 学习汇总5 - index 创建( tcy)

index 创建  2018/12/2

#1.pd.Index
i=pd.Index([1,2,3,4]) # (Int64Index([1, 2, 3, 4], dtype='int64')
i=pd.Index(list('abcd')) # Index(['a','b','c','d'], dtype='object'))

#2.pd.RangeIdex(start=None, stop=None, step=None)迭代器
i=pd.RangeIndex(4) # RangeIndex(start=0, stop=4, step=1)
[i1 for i1 in i] #[0, 1, 2, 3, 4]

#3.pd.date_range(start=None, end=None, periods=None, freq=None)
# freq:#Y年显示每年的最后一天;M显示每月的最后一天;D天;H小时;60S一分钟

date=pd.date_range('2018-10-13',periods=3)
# DatetimeIndex(['2018-10-14','2018-10-15','2018-10-16'],dtype='datetime64[ns]',freq='D')
i_str=date.strftime('%Y-%m-%d %H:%M:%S')#将日期index变成字符串index
# Index(['2018-10-14 00:00:00','2018-10-15 00:00:00','2018-10-16 00:00:00'],dtype='object')
#4.pd.DatetimeIndex(data=None, freq=None, start=None, end=None, periods=None, ...)  
dt=pd.DatetimeIndex(['2018-10-14', '2018-10-15', '2018-10-16'])
         # DatetimeIndex(['2018-10-14', '2018-10-15', '2018-10-16''], dtype='datetime64[ns]', freq=None)
    
s=pd.Series(data=np.arange(10,14),index=pd.date_range('2018-10-14',periods=4),dtype=np.float, name='Series-1')
s.index  # DatetimeIndex(['2018-10-14', '2018-10-15', '2018-10-16'], dtype='datetime64[ns]', freq='D')
s
             # 2018-10-14    10.0
             # 2018-10-15    11.0
             # 2018-10-16    12.0
             # Freq: D, Name: Series-1, dtype: float64

 

你可能感兴趣的:(pandas)