4.1 Pandas中的数据结构1.DataFrame的创建【例4-12】DataFrame创建时指定列名In[12]:df3 = pd.DataFrame(data, columns = ['name', 'sex''year', 'city'], index = ['a', "b', 'c', 'd')print(df3)sex year citynameOut[12]:张三female 2001北京ab李四female2001上海C王五male 2003 广州d小明male 2002北京
4.1 Pandas中的数据结构 1. DataFrame的创建 【例4-12】DataFrame创建时指定列名 In[12]: df3 = pd.DataFrame(data, columns = ['name', 'sex', 'year', 'city'], index = ['a', 'b', 'c', 'd']) print(df3) Out[12]: name sex year city a 张三 female 2001 北京 b 李四 female 2001 上海 c 王五 male 2003 广州 d 小明 male 2002 北京
4.1Pandas中的数据结构2.DataFrame的属性函数返回值元素values索引index列名columns类型dtypessize元素个数维度数ndimshape数据形状(行列数目)
4.1 Pandas中的数据结构 2. DataFrame的属性 函数 返回值 values 元素 index 索引 columns 列名 dtypes 类型 size 元素个数 ndim 维度数 shape 数据形状(行列数目)
4.1 Pandas中的数据结构*4.1.3索引对象Pandas的索引对象负责管理轴标签和其他元数据(比如轴名称等)。构建Series或DataFrame时,所用到的任何数组或其他序列的标签都会被转换成一个Index
❖4.1.3 索引对象 Pandas的索引对象负责管理轴标签和其他元数据(比如轴名 称等)。构建Series或 DataFrame时,所用到的任何数组或其他 序列的标签都会被转换成一个Index。 4.1 Pandas中的数据结构
4.1 Pandas中的数据结构*4.1.3索引对象【例4-13】显示DataFrame的索引和列。In[13]:print(df)print(df.index)print(df.columns)Out[13]:namesex year city张三female2001北京ab李四female2001上海c王五male2003广州d小明male2002北京Index(['a', "b','c', 'd'l, dtype ='object")Index(['name','sex','year,'city'l, dtype ='object")
❖4.1.3 索引对象 【例4-13】显示DataFrame的索引和列。 4.1 Pandas中的数据结构 In[13]: print(df) print(df.index) print(df.columns) Out[13]: name sex year city a 张三 female 2001 北京 b 李四 female 2001 上海 c 王五 male 2003 广州 d 小明 male 2002 北京 Index(['a', 'b', 'c', 'd'], dtype = 'object') Index(['name', 'sex', 'year', 'city'], dtype = 'object')
4.1 Pandas中的数据结构*4.1.3 索引对象【例4-14】DataFrame的lndex。In[14]:print(name'indf.columns)print(a' in df.index)TrueOut[14]:False
❖4.1.3 索引对象 【例4-14】DataFrame的Index。 4.1 Pandas中的数据结构 In[14]: print('name' in df.columns) print('a' in df.index) Out[14]: True False