python推荐菜品模型,CH6 数据选择(菜品挑选).ipynb

{

"cells": [

{

"cell_type": "markdown",

"metadata": {},

"source": [

"# CH6 数据选择(菜品挑选)"

]

},

{

"cell_type": "markdown",

"metadata": {},

"source": [

"数据选择,简单地说就是把需要的数据做筛选操作,主要有以下三种方式:
\n",

"1.列选择
\n",

"2.行选择
\n",

"3.行列同时选择
"

]

},

{

"cell_type": "code",

"execution_count": 2,

"metadata": {},

"outputs": [],

"source": [

"import pandas as pd"

]

},

{

"cell_type": "markdown",

"metadata": {},

"source": [

"## 6.1列选择"

]

},

{

"cell_type": "markdown",

"metadata": {},

"source": [

"在Python中,若只需要选出部分列,可以直接在df后面的方括号中指明列名或是用列表传递多个值"

]

},

{

"cell_type": "code",

"execution_count": 3,

"metadata": {},

"outputs": [

{

"data": {

"text/html": [

"

\n",

"

" .dataframe tbody tr th:only-of-type {\n",

" vertical-align: middle;\n",

" }\n",

"\n",

" .dataframe tbody tr th {\n",

" vertical-align: top;\n",

" }\n",

"\n",

" .dataframe thead th {\n",

" text-align: right;\n",

" }\n",

"\n",

"

" \n",

"

\n",

"

\n",

"

订单编号\n",

"

客户姓名\n",

"

\n",

"

\n",

"

\n",

"

\n",

"

0\n",

"

A1\n",

"

张通\n",

"

\n",

"

\n",

"

1\n",

"

A2\n",

"

李谷\n",

"

\n",

"

\n",

"

2\n",

"

A3\n",

"

孙凤\n",

"

\n",

"

\n",

"

3\n",

"

A4\n",

"

赵恒\n",

"

\n",

"

\n",

"

4\n",

"

A5\n",

"

赵恒\n",

"

\n",

"

\n",

"

\n",

"

"

],

"text/plain": [

" 订单编号 客户姓名\n",

"0 A1 张通\n",

"1 A2 李谷\n",

"2 A3 孙凤\n",

"3 A4 赵恒\n",

"4 A5 赵恒"

]

},

"execution_count": 3,

"metadata": {},

"output_type": "execute_result"

}

],

"source": [

"data_dict = {\"订单编号\":[\"A1\",\"A2\",\"A3\",\"A4\",\"A5\"],\n",

" \"客户姓名\":[\"张通\",\"李谷\",\"孙凤\",\"赵恒\",\"赵恒\"],\n",

" \"唯一识别码\":[101,102,103,104,104],\n",

" \"成交时间\":[\"2018-8-8\",\"2018-8-9\",\"2018-8-10\",\"2018-8-11\",\"2018-8-12\"]}\n",

"df = pd.DataFrame(data_dict)\n",

"df[[\"订单编号\",\"客户姓名\"]]"

]

},

{

"cell_type": "markdown",

"metadata": {},

"source": [

"用列明进行筛选的方式称之为普通索引
\n",

"还可以用iloc方法传入具体列的位置,对数据进行筛选
\n",

"iloc[ , ]的方括号中分别表示行与列的选取,同样也是从0开始计数
\n",

"比如说,0:3代表选择第1列到第4列之间的值,用区间表示相当于是[0, 4)
"

]

},

{

"cell_type": "code",

"execution_count": 4,

"metadata": {},

"outputs": [

{

"data": {

"text/html": [

"

\n",

"

" .dataframe tbody tr th:only-of-type {\n",

" vertical-align: middle;\n",

" }\n",

"\n",

" .dataframe tbody tr th {\n",

" vertical-align: top;\n",

" }\n",

"\n",

" .dataframe thead th {\n",

" text-align: right;\n",

" }\n",

"\n",

"

" \n",

"

\n",

"

\n",

"

订单编号\n",

"

客户姓名\n",

"

唯一识别码\n",

"

\n",

"

\n",

"

\n",

"

\n",

"

0\n",

"

A1\n",

"

张通\n",

"

101\n",

"

\n",

"

\n",

"

1\n",

"

A2\n",

"

李谷\n",

"

102\n",

"

\n",

"

\n",

"

2\n",

"

A3\n",

"

孙凤\n",

"

103\n",

"

\n",

"

\n",

"

3\n",

"

A4\n",

"

赵恒\n",

"

104\n",

"

\n",

"

\n",

"

4\n",

"

A5\n",

"

赵恒\n",

"

104\n",

"

\n",

"

\n",

"

\n",

"

"

],

"text/plain": [

" 订单编号 客户姓名 唯一识别码\n",

"0 A1 张通 101\n",

"1 A2 李谷 102\n",

"2 A3 孙凤 103\n",

"3 A4 赵恒 104\n",

"4 A5 赵恒 104"

]

},

"execution_count": 4,

"metadata": {},

"output_type": "execute_result"

}

],

"source": [

"data_dict = {\"订单编号\":[\"A1\",\"A2\",\"A3\",\"A4\",\"A5\"],\n",

" \"客户姓名\":[\"张通\",\"李谷\",\"孙凤\",\"赵恒\",\"赵恒\"],\n",

" \"唯一识别码\":[101,102,103,104,104],\n",

" \"成交时间\":[\"2018-8-8\",\"2018-8-9\",\"2018-8-10\",\"2018-8-11\",\"2018-8-12\"]}\n",

"df = pd.DataFrame(data_dict)\n",

"df.iloc[:, 0:3]"

]

},

{

"cell_type": "markdown",

"metadata": {},

"source": [

"## 6.2行选择"

]

},

{

"cell_type": "markdown",

"metadata": {},

"source": [

"与列选择类似,行选择也有两种方法:
\n",

"1.普通索引,传入具体行索引的名称,要用到loc方法
\n",

"2.位置索引,传入具体的行数,要用到iloc方法;
"

]

},

{

"cell_type": "code",

"execution_count": 5,

"metadata": {},

"outputs": [],

"source": [

"data_dict = {\"订单编号\":[\"A1\",\"A2\",\"A3\",\"A4\",\"A5\"],\n",

" \"客户姓名\":[\"张通\",\"李谷\",\"孙凤\",\"赵恒\",\"赵恒\"],\n",

" \"唯一识别码\":[101,102,103,104,104],\n",

" \"成交时间\":[\"2018-8-8\",\"2018-8-9\",\"2018-8-10\",\"2018-8-11\",\"2018-8-12\"]}\n",

"df = pd.DataFrame(data_dict)\n",

"df.index = [\"一\",\"二\",\"三\",\"四\",\"五\"]"

]

},

{

"cell_type": "markdown",

"metadata": {},

"source": [

"为了演示方便,对索引重新进行赋值"

]

},

{

"cell_type": "code",

"execution_count": 6,

"metadata": {},

"outputs": [

{

"data": {

"text/plain": [

"订单编号 A1\n",

"客户姓名 张通\n",

"唯一识别码 101\n",

"成交时间 2018-8-8\n",

"Name: 一, dtype: object"

]

},

"execution_count": 6,

"metadata": {},

"output_type": "execute_result"

}

],

"source": [

"df.loc[\"一\"]"

]

},

{

"cell_type": "code",

"execution_count": 7,

"metadata": {},

"outputs": [

{

"data": {

"text/html": [

"

\n",

"

" .dataframe tbody tr th:only-of-type {\n",

" vertical-align: middle;\n",

" }\n",

"\n",

" .dataframe tbody tr th {\n",

" vertical-align: top;\n",

" }\n",

"\n",

" .dataframe thead th {\n",

" text-align: right;\n",

" }\n",

"\n",

"

" \n",

"

\n",

"

\n",

"

订单编号\n",

"

客户姓名\n",

"

唯一识别码\n",

"

成交时间\n",

"

\n",

"

\n",

"

\n",

"

\n",

"

一\n",

"

A1\n",

"

张通\n",

"

101\n",

"

2018-8-8\n",

"

\n",

"

\n",

"

二\n",

"

A2\n",

"

李谷\n",

"

102\n",

"

2018-8-9\n",

"

\n",

"

\n",

"

\n",

"

"

],

"text/plain": [

" 订单编号 客户姓名 唯一识别码 成交时间\n",

"一 A1 张通 101 2018-8-8\n",

"二 A2 李谷 102 2018-8-9"

]

},

"execution_count": 7,

"metadata": {},

"output_type": "execute_result"

}

],

"source": [

"df.loc[[\"一\",\"二\"]]"

]

},

{

"cell_type": "code",

"execution_count": 8,

"metadata": {},

"outputs": [

{

"data": {

"text/plain": [

"订单编号 A1\n",

"客户姓名 张通\n",

"唯一识别码 101\n",

"成交时间 2018-8-8\n",

"Name: 一, dtype: object"

]

},

"execution_count": 8,

"metadata": {},

"output_type": "execute_result"

}

],

"source": [

"df.iloc[0]"

]

},

{

"cell_type": "code",

"execution_count": 9,

"metadata": {},

"outputs": [

{

"data": {

"text/html": [

"

\n",

"

" .dataframe tbody tr th:only-of-type {\n",

" vertical-align: middle;\n",

" }\n",

"\n",

" .dataframe tbody tr th {\n",

" vertical-align: top;\n",

" }\n",

"\n",

" .dataframe thead th {\n",

" text-align: right;\n",

" }\n",

"\n",

"

" \n",

"

\n",

"

\n",

"

订单编号\n",

"

客户姓名\n",

"

唯一识别码\n",

"

成交时间\n",

"

\n",

"

\n",

"

\n",

"

\n",

"

一\n",

"

A1\n",

"

张通\n",

"

101\n",

"

2018-8-8\n",

"

\n",

"

\n",

"

二\n",

"

A2\n",

"

李谷\n",

"

102\n",

"

2018-8-9\n",

"

\n",

"

\n",

"

\n",

"

"

],

"text/plain": [

" 订单编号 客户姓名 唯一识别码 成交时间\n",

"一 A1 张通 101 2018-8-8\n",

"二 A2 李谷 102 2018-8-9"

]

},

"execution_count": 9,

"metadata": {},

"output_type": "execute_result"

}

],

"source": [

"df.iloc[[0,1]]"

]

},

{

"cell_type": "code",

"execution_count": 10,

"metadata": {},

"outputs": [

{

"data": {

"text/html": [

"

\n",

"

" .dataframe tbody tr th:only-of-type {\n",

" vertical-align: middle;\n",

" }\n",

"\n",

" .dataframe tbody tr th {\n",

" vertical-align: top;\n",

" }\n",

"\n",

" .dataframe thead th {\n",

" text-align: right;\n",

" }\n",

"\n",

"

" \n",

"

\n",

"

\n",

"

订单编号\n",

"

客户姓名\n",

"

唯一识别码\n",

"

成交时间\n",

"

\n",

"

\n",

"

\n",

"

\n",

"

一\n",

"

A1\n",

"

张通\n",

"

101\n",

"

2018-8-8\n",

"

\n",

"

\n",

"

二\n",

"

A2\n",

"

李谷\n",

"

102\n",

"

2018-8-9\n",

"

\n",

"

\n",

"

\n",

"

"

],

"text/plain": [

" 订单编号 客户姓名 唯一识别码 成交时间\n",

"一 A1 张通 101 2018-8-8\n",

"二 A2 李谷 102 2018-8-9"

]

},

"execution_count": 10,

"metadata": {},

"output_type": "execute_result"

}

],

"source": [

"df.iloc[0:2]"

]

},

{

"cell_type": "markdown",

"metadata": {},

"source": [

"当然,也可以通过判断条件来对行进行筛选,这种称为布尔索引"

]

},

{

"cell_type": "code",

"execution_count": 11,

"metadata": {},

"outputs": [],

"source": [

"data_dict = {\"订单编号\":[\"A1\",\"A2\",\"A3\",\"A4\",\"A5\"],\n",

" \"客户姓名\":[\"张通\",\"李谷\",\"孙凤\",\"赵恒\",\"赵恒\"],\n",

" \"唯一识别码\":[101,102,103,104,104],\n",

" \"年龄\":[31,45,23,240,240],\n",

" \"成交时间\":[\"2018-8-8\",\"2018-8-9\",\"2018-8-10\",\"2018-8-11\",\"2018-8-12\"]}\n",

"df = pd.DataFrame(data_dict)"

]

},

{

"cell_type": "code",

"execution_count": 12,

"metadata": {},

"outputs": [

{

"data": {

"text/html": [

"

\n",

"

" .dataframe tbody tr th:only-of-type {\n",

" vertical-align: middle;\n",

" }\n",

"\n",

" .dataframe tbody tr th {\n",

" vertical-align: top;\n",

" }\n",

"\n",

" .dataframe thead th {\n",

" text-align: right;\n",

" }\n",

"\n",

"

" \n",

"

\n",

"

\n",

"

订单编号\n",

"

客户姓名\n",

"

唯一识别码\n",

"

年龄\n",

"

成交时间\n",

"

\n",

"

\n",

"

\n",

"

\n",

"

0\n",

"

A1\n",

"

张通\n",

"

101\n",

"

31\n",

"

2018-8-8\n",

"

\n",

"

\n",

"

1\n",

"

A2\n",

"

李谷\n",

"

102\n",

"

45\n",

"

2018-8-9\n",

"

\n",

"

\n",

"

2\n",

"

A3\n",

"

孙凤\n",

"

103\n",

"

23\n",

"

2018-8-10\n",

"

\n",

"

\n",

"

\n",

"

"

],

"text/plain": [

" 订单编号 客户姓名 唯一识别码 年龄 成交时间\n",

"0 A1 张通 101 31 2018-8-8\n",

"1 A2 李谷 102 45 2018-8-9\n",

"2 A3 孙凤 103 23 2018-8-10"

]

},

"execution_count": 12,

"metadata": {},

"output_type": "execute_result"

}

],

"source": [

"df[df[\"年龄\"]< 200]"

]

},

{

"cell_type": "code",

"execution_count": 13,

"metadata": {},

"outputs": [

{

"data": {

"text/html": [

"

\n",

"

" .dataframe tbody tr th:only-of-type {\n",

" vertical-align: middle;\n",

" }\n",

"\n",

" .dataframe tbody tr th {\n",

" vertical-align: top;\n",

" }\n",

"\n",

" .dataframe thead th {\n",

" text-align: right;\n",

" }\n",

"\n",

"

" \n",

"

\n",

"

\n",

"

订单编号\n",

"

客户姓名\n",

"

唯一识别码\n",

"

年龄\n",

"

成交时间\n",

"

\n",

"

\n",

"

\n",

"

\n",

"

0\n",

"

A1\n",

"

张通\n",

"

101\n",

"

31\n",

"

2018-8-8\n",

"

\n",

"

\n",

"

\n",

"

"

],

"text/plain": [

" 订单编号 客户姓名 唯一识别码 年龄 成交时间\n",

"0 A1 张通 101 31 2018-8-8"

]

},

"execution_count": 13,

"metadata": {},

"output_type": "execute_result"

}

],

"source": [

"df[(df[\"年龄\"] < 200) & (df[\"唯一识别码\"] < 102)]"

]

},

{

"cell_type": "markdown",

"metadata": {},

"source": [

"## 6.3行与列同时选择"

]

},

{

"cell_type": "code",

"execution_count": 14,

"metadata": {},

"outputs": [],

"source": [

"data_dict = {\"订单编号\":[\"A1\",\"A2\",\"A3\",\"A4\",\"A5\"],\n",

" \"客户姓名\":[\"张通\",\"李谷\",\"孙凤\",\"赵恒\",\"赵恒\"],\n",

" \"唯一识别码\":[101,102,103,104,104],\n",

" \"成交时间\":[\"2018-8-8\",\"2018-8-9\",\"2018-8-10\",\"2018-8-11\",\"2018-8-12\"]}\n",

"df = pd.DataFrame(data_dict)\n",

"df.index = [\"一\",\"二\",\"三\",\"四\",\"五\"]"

]

},

{

"cell_type": "code",

"execution_count": 15,

"metadata": {},

"outputs": [

{

"data": {

"text/html": [

"

\n",

"

" .dataframe tbody tr th:only-of-type {\n",

" vertical-align: middle;\n",

" }\n",

"\n",

" .dataframe tbody tr th {\n",

" vertical-align: top;\n",

" }\n",

"\n",

" .dataframe thead th {\n",

" text-align: right;\n",

" }\n",

"\n",

"

" \n",

"

\n",

"

\n",

"

订单编号\n",

"

客户姓名\n",

"

唯一识别码\n",

"

成交时间\n",

"

\n",

"

\n",

"

\n",

"

\n",

"

一\n",

"

A1\n",

"

张通\n",

"

101\n",

"

2018-8-8\n",

"

\n",

"

\n",

"

二\n",

"

A2\n",

"

李谷\n",

"

102\n",

"

2018-8-9\n",

"

\n",

"

\n",

"

三\n",

"

A3\n",

"

孙凤\n",

"

103\n",

"

2018-8-10\n",

"

\n",

"

\n",

"

四\n",

"

A4\n",

"

赵恒\n",

"

104\n",

"

2018-8-11\n",

"

\n",

"

\n",

"

五\n",

"

A5\n",

"

赵恒\n",

"

104\n",

"

2018-8-12\n",

"

\n",

"

\n",

"

\n",

"

"

],

"text/plain": [

" 订单编号 客户姓名 唯一识别码 成交时间\n",

"一 A1 张通 101 2018-8-8\n",

"二 A2 李谷 102 2018-8-9\n",

"三 A3 孙凤 103 2018-8-10\n",

"四 A4 赵恒 104 2018-8-11\n",

"五 A5 赵恒 104 2018-8-12"

]

},

"execution_count": 15,

"metadata": {},

"output_type": "execute_result"

}

],

"source": [

"df"

]

},

{

"cell_type": "markdown",

"metadata": {},

"source": [

"### 6.3.1普通索引+普通索引"

]

},

{

"cell_type": "markdown",

"metadata": {},

"source": [

"同时传入行与列的索引名进行数据选择,用loc方法"

]

},

{

"cell_type": "code",

"execution_count": 16,

"metadata": {},

"outputs": [

{

"data": {

"text/html": [

"

\n",

"

" .dataframe tbody tr th:only-of-type {\n",

" vertical-align: middle;\n",

" }\n",

"\n",

" .dataframe tbody tr th {\n",

" vertical-align: top;\n",

" }\n",

"\n",

" .dataframe thead th {\n",

" text-align: right;\n",

" }\n",

"\n",

"

" \n",

"

\n",

"

\n",

"

订单编号\n",

"

唯一识别码\n",

"

\n",

"

\n",

"

\n",

"

\n",

"

一\n",

"

A1\n",

"

101\n",

"

\n",

"

\n",

"

三\n",

"

A3\n",

"

103\n",

"

\n",

"

\n",

"

\n",

"

"

],

"text/plain": [

" 订单编号 唯一识别码\n",

"一 A1 101\n",

"三 A3 103"

]

},

"execution_count": 16,

"metadata": {},

"output_type": "execute_result"

}

],

"source": [

"df.loc[[\"一\",\"三\"],[\"订单编号\",\"唯一识别码\"]]"

]

},

{

"cell_type": "markdown",

"metadata": {},

"source": [

"### 6.3.2位置索引+位置索引"

]

},

{

"cell_type": "markdown",

"metadata": {},

"source": [

"同时传入行与列的位置索引名进行数据选择,用iloc方法
\n",

"左边的方括号表示行索引选择,右边的方括号表示列索引选择
\n",

"都是从0开始计数的"

]

},

{

"cell_type": "code",

"execution_count": 17,

"metadata": {},

"outputs": [

{

"data": {

"text/html": [

"

\n",

"

" .dataframe tbody tr th:only-of-type {\n",

" vertical-align: middle;\n",

" }\n",

"\n",

" .dataframe tbody tr th {\n",

" vertical-align: top;\n",

" }\n",

"\n",

" .dataframe thead th {\n",

" text-align: right;\n",

" }\n",

"\n",

"

" \n",

"

\n",

"

\n",

"

订单编号\n",

"

唯一识别码\n",

"

\n",

"

\n",

"

\n",

"

\n",

"

一\n",

"

A1\n",

"

101\n",

"

\n",

"

\n",

"

二\n",

"

A2\n",

"

102\n",

"

\n",

"

\n",

"

\n",

"

"

],

"text/plain": [

" 订单编号 唯一识别码\n",

"一 A1 101\n",

"二 A2 102"

]

},

"execution_count": 17,

"metadata": {},

"output_type": "execute_result"

}

],

"source": [

"df.iloc[[0,1],[0,2]]"

]

},

{

"cell_type": "markdown",

"metadata": {},

"source": [

"### 6.3.3布尔索引+普通索引"

]

},

{

"cell_type": "markdown",

"metadata": {},

"source": [

"逻辑:先用布尔索引选择行,再通过普通索引选择列"

]

},

{

"cell_type": "code",

"execution_count": 18,

"metadata": {},

"outputs": [],

"source": [

"data_dict = {\"订单编号\":[\"A1\",\"A2\",\"A3\",\"A4\",\"A5\"],\n",

" \"客户姓名\":[\"张通\",\"李谷\",\"孙凤\",\"赵恒\",\"赵恒\"],\n",

" \"唯一识别码\":[101,102,103,104,104],\n",

" \"年龄\":[31,45,23,240,240],\n",

" \"成交时间\":[\"2018-8-8\",\"2018-8-9\",\"2018-8-10\",\"2018-8-11\",\"2018-8-12\"]}\n",

"df = pd.DataFrame(data_dict)"

]

},

{

"cell_type": "code",

"execution_count": 19,

"metadata": {},

"outputs": [

{

"data": {

"text/html": [

"

\n",

"

" .dataframe tbody tr th:only-of-type {\n",

" vertical-align: middle;\n",

" }\n",

"\n",

" .dataframe tbody tr th {\n",

" vertical-align: top;\n",

" }\n",

"\n",

" .dataframe thead th {\n",

" text-align: right;\n",

" }\n",

"\n",

"

" \n",

"

\n",

"

\n",

"

订单编号\n",

"

年龄\n",

"

\n",

"

\n",

"

\n",

"

\n",

"

0\n",

"

A1\n",

"

31\n",

"

\n",

"

\n",

"

1\n",

"

A2\n",

"

45\n",

"

\n",

"

\n",

"

2\n",

"

A3\n",

"

23\n",

"

\n",

"

\n",

"

\n",

"

"

],

"text/plain": [

" 订单编号 年龄\n",

"0 A1 31\n",

"1 A2 45\n",

"2 A3 23"

]

},

"execution_count": 19,

"metadata": {},

"output_type": "execute_result"

}

],

"source": [

"df[df[\"年龄\"] < 200][[\"订单编号\",\"年龄\"]] "

]

},

{

"cell_type": "markdown",

"metadata": {},

"source": [

"### 6.3.4切片索引+切片索引"

]

},

{

"cell_type": "markdown",

"metadata": {},

"source": [

"同时传入行,列索引的位置索引进行数据选择,用iloc方法"

]

},

{

"cell_type": "code",

"execution_count": 21,

"metadata": {},

"outputs": [

{

"data": {

"text/html": [

"

\n",

"

" .dataframe tbody tr th:only-of-type {\n",

" vertical-align: middle;\n",

" }\n",

"\n",

" .dataframe tbody tr th {\n",

" vertical-align: top;\n",

" }\n",

"\n",

" .dataframe thead th {\n",

" text-align: right;\n",

" }\n",

"\n",

"

" \n",

"

\n",

"

\n",

"

订单编号\n",

"

客户姓名\n",

"

唯一识别码\n",

"

成交时间\n",

"

\n",

"

\n",

"

\n",

"

\n",

"

一\n",

"

A1\n",

"

张通\n",

"

101\n",

"

2018-8-8\n",

"

\n",

"

\n",

"

二\n",

"

A2\n",

"

李谷\n",

"

102\n",

"

2018-8-9\n",

"

\n",

"

\n",

"

三\n",

"

A3\n",

"

孙凤\n",

"

103\n",

"

2018-8-10\n",

"

\n",

"

\n",

"

四\n",

"

A4\n",

"

赵恒\n",

"

104\n",

"

2018-8-11\n",

"

\n",

"

\n",

"

五\n",

"

A5\n",

"

赵恒\n",

"

104\n",

"

2018-8-12\n",

"

\n",

"

\n",

"

\n",

"

"

],

"text/plain": [

" 订单编号 客户姓名 唯一识别码 成交时间\n",

"一 A1 张通 101 2018-8-8\n",

"二 A2 李谷 102 2018-8-9\n",

"三 A3 孙凤 103 2018-8-10\n",

"四 A4 赵恒 104 2018-8-11\n",

"五 A5 赵恒 104 2018-8-12"

]

},

"execution_count": 21,

"metadata": {},

"output_type": "execute_result"

}

],

"source": [

"data_dict = {\"订单编号\":[\"A1\",\"A2\",\"A3\",\"A4\",\"A5\"],\n",

" \"客户姓名\":[\"张通\",\"李谷\",\"孙凤\",\"赵恒\",\"赵恒\"],\n",

" \"唯一识别码\":[101,102,103,104,104],\n",

" \"成交时间\":[\"2018-8-8\",\"2018-8-9\",\"2018-8-10\",\"2018-8-11\",\"2018-8-12\"]}\n",

"df = pd.DataFrame(data_dict)\n",

"df.index = [\"一\",\"二\",\"三\",\"四\",\"五\"]\n",

"df"

]

},

{

"cell_type": "code",

"execution_count": 22,

"metadata": {},

"outputs": [

{

"data": {

"text/html": [

"

\n",

"

" .dataframe tbody tr th:only-of-type {\n",

" vertical-align: middle;\n",

" }\n",

"\n",

" .dataframe tbody tr th {\n",

" vertical-align: top;\n",

" }\n",

"\n",

" .dataframe thead th {\n",

" text-align: right;\n",

" }\n",

"\n",

"

" \n",

"

\n",

"

\n",

"

客户姓名\n",

"

唯一识别码\n",

"

\n",

"

\n",

"

\n",

"

\n",

"

一\n",

"

张通\n",

"

101\n",

"

\n",

"

\n",

"

二\n",

"

李谷\n",

"

102\n",

"

\n",

"

\n",

"

三\n",

"

孙凤\n",

"

103\n",

"

\n",

"

\n",

"

\n",

"

"

],

"text/plain": [

" 客户姓名 唯一识别码\n",

"一 张通 101\n",

"二 李谷 102\n",

"三 孙凤 103"

]

},

"execution_count": 22,

"metadata": {},

"output_type": "execute_result"

}

],

"source": [

"df.iloc[0:3,1:3]"

]

},

{

"cell_type": "markdown",

"metadata": {},

"source": [

"### 6.3.5切片索引+普通索引"

]

},

{

"cell_type": "code",

"execution_count": 23,

"metadata": {},

"outputs": [],

"source": [

"data_dict = {\"订单编号\":[\"A1\",\"A2\",\"A3\",\"A4\",\"A5\"],\n",

" \"客户姓名\":[\"张通\",\"李谷\",\"孙凤\",\"赵恒\",\"赵恒\"],\n",

" \"唯一识别码\":[101,102,103,104,104],\n",

" \"成交时间\":[\"2018-8-8\",\"2018-8-9\",\"2018-8-10\",\"2018-8-11\",\"2018-8-12\"]}\n",

"df = pd.DataFrame(data_dict)\n",

"df.index = [\"一\",\"二\",\"三\",\"四\",\"五\"]"

]

},

{

"cell_type": "code",

"execution_count": 24,

"metadata": {},

"outputs": [

{

"name": "stderr",

"output_type": "stream",

"text": [

"D:\\Anaconda3\\lib\\site-packages\\ipykernel_launcher.py:1: DeprecationWarning: \n",

".ix is deprecated. Please use\n",

".loc for label based indexing or\n",

".iloc for positional indexing\n",

"\n",

"See the documentation here:\n",

"http://pandas.pydata.org/pandas-docs/stable/indexing.html#ix-indexer-is-deprecated\n",

" \"\"\"Entry point for launching an IPython kernel.\n"

]

},

{

"data": {

"text/html": [

"

\n",

"

" .dataframe tbody tr th:only-of-type {\n",

" vertical-align: middle;\n",

" }\n",

"\n",

" .dataframe tbody tr th {\n",

" vertical-align: top;\n",

" }\n",

"\n",

" .dataframe thead th {\n",

" text-align: right;\n",

" }\n",

"\n",

"

" \n",

"

\n",

"

\n",

"

客户姓名\n",

"

唯一识别码\n",

"

\n",

"

\n",

"

\n",

"

\n",

"

一\n",

"

张通\n",

"

101\n",

"

\n",

"

\n",

"

二\n",

"

李谷\n",

"

102\n",

"

\n",

"

\n",

"

\n",

"

"

],

"text/plain": [

" 客户姓名 唯一识别码\n",

"一 张通 101\n",

"二 李谷 102"

]

},

"execution_count": 24,

"metadata": {},

"output_type": "execute_result"

}

],

"source": [

"df.ix[0:2, [\"客户姓名\",\"唯一识别码\"]]"

]

}

],

"metadata": {

"kernelspec": {

"display_name": "Python 3",

"language": "python",

"name": "python3"

},

"language_info": {

"codemirror_mode": {

"name": "ipython",

"version": 3

},

"file_extension": ".py",

"mimetype": "text/x-python",

"name": "python",

"nbconvert_exporter": "python",

"pygments_lexer": "ipython3",

"version": "3.7.1"

},

"toc": {

"base_numbering": 1,

"nav_menu": {},

"number_sections": false,

"sideBar": true,

"skip_h1_title": false,

"title_cell": "Table of Contents",

"title_sidebar": "Contents",

"toc_cell": false,

"toc_position": {

"height": "calc(100% - 180px)",

"left": "10px",

"top": "150px",

"width": "270.069px"

},

"toc_section_display": true,

"toc_window_display": true

}

},

"nbformat": 4,

"nbformat_minor": 2

}

一键复制

编辑

Web IDE

原始数据

按行查看

历史

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值