Skip to content

Commit b74f5b0

Browse files
committed
additional snippets
1 parent d796f64 commit b74f5b0

10 files changed

+1756
-50
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
{
2+
"metadata": {
3+
"name": "",
4+
"signature": "sha256:ba93d504e655c9a9dcb910c6d748ac9f7b4f631c7c41569ea525f06066433a39"
5+
},
6+
"nbformat": 3,
7+
"nbformat_minor": 0,
8+
"worksheets": [
9+
{
10+
"cells": [
11+
{
12+
"cell_type": "markdown",
13+
"metadata": {},
14+
"source": [
15+
"# List Unique Values In A Pandas Column\n",
16+
"\n",
17+
"This snippit was written by [Chris R. Albon](http://www.chrisralbon.com/) and is part of his collection of [well-documented Python snippits](https://github.com/chrisalbon/code_py). All code is written in Python 3 in iPython notebook and offered under the [Creative Commons Attribution-ShareAlike 4.0 International License](http://creativecommons.org/licenses/by-sa/4.0/\"><img alt=\"Creative Commons License)."
18+
]
19+
},
20+
{
21+
"cell_type": "markdown",
22+
"metadata": {},
23+
"source": [
24+
"## Preliminaries"
25+
]
26+
},
27+
{
28+
"cell_type": "code",
29+
"collapsed": false,
30+
"input": [
31+
"# Import modules\n",
32+
"import pandas as pd\n",
33+
"\n",
34+
"# Set ipython's max row display\n",
35+
"pd.set_option('display.max_row', 1000)\n",
36+
"\n",
37+
"# Set iPython's max column width to 50\n",
38+
"pd.set_option('display.max_columns', 50)"
39+
],
40+
"language": "python",
41+
"metadata": {},
42+
"outputs": [],
43+
"prompt_number": 6
44+
},
45+
{
46+
"cell_type": "markdown",
47+
"metadata": {},
48+
"source": [
49+
"## Create an example dataframe"
50+
]
51+
},
52+
{
53+
"cell_type": "code",
54+
"collapsed": false,
55+
"input": [
56+
"# Create an example dataframe\n",
57+
"data = {'name': ['Jason', 'Molly', 'Tina', 'Jake', 'Amy'], \n",
58+
" 'year': [2012, 2012, 2013, 2014, 2014], \n",
59+
" 'reports': [4, 24, 31, 2, 3]}\n",
60+
"df = pd.DataFrame(data, index = ['Cochice', 'Pima', 'Santa Cruz', 'Maricopa', 'Yuma'])\n",
61+
"df"
62+
],
63+
"language": "python",
64+
"metadata": {},
65+
"outputs": [
66+
{
67+
"html": [
68+
"<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
69+
"<table border=\"1\" class=\"dataframe\">\n",
70+
" <thead>\n",
71+
" <tr style=\"text-align: right;\">\n",
72+
" <th></th>\n",
73+
" <th>name</th>\n",
74+
" <th>reports</th>\n",
75+
" <th>year</th>\n",
76+
" </tr>\n",
77+
" </thead>\n",
78+
" <tbody>\n",
79+
" <tr>\n",
80+
" <th>Cochice</th>\n",
81+
" <td> Jason</td>\n",
82+
" <td> 4</td>\n",
83+
" <td> 2012</td>\n",
84+
" </tr>\n",
85+
" <tr>\n",
86+
" <th>Pima</th>\n",
87+
" <td> Molly</td>\n",
88+
" <td> 24</td>\n",
89+
" <td> 2012</td>\n",
90+
" </tr>\n",
91+
" <tr>\n",
92+
" <th>Santa Cruz</th>\n",
93+
" <td> Tina</td>\n",
94+
" <td> 31</td>\n",
95+
" <td> 2013</td>\n",
96+
" </tr>\n",
97+
" <tr>\n",
98+
" <th>Maricopa</th>\n",
99+
" <td> Jake</td>\n",
100+
" <td> 2</td>\n",
101+
" <td> 2014</td>\n",
102+
" </tr>\n",
103+
" <tr>\n",
104+
" <th>Yuma</th>\n",
105+
" <td> Amy</td>\n",
106+
" <td> 3</td>\n",
107+
" <td> 2014</td>\n",
108+
" </tr>\n",
109+
" </tbody>\n",
110+
"</table>\n",
111+
"</div>"
112+
],
113+
"metadata": {},
114+
"output_type": "pyout",
115+
"prompt_number": 7,
116+
"text": [
117+
" name reports year\n",
118+
"Cochice Jason 4 2012\n",
119+
"Pima Molly 24 2012\n",
120+
"Santa Cruz Tina 31 2013\n",
121+
"Maricopa Jake 2 2014\n",
122+
"Yuma Amy 3 2014"
123+
]
124+
}
125+
],
126+
"prompt_number": 7
127+
},
128+
{
129+
"cell_type": "markdown",
130+
"metadata": {},
131+
"source": [
132+
"## List unique values"
133+
]
134+
},
135+
{
136+
"cell_type": "code",
137+
"collapsed": false,
138+
"input": [
139+
"#List unique values in the df['name'] column\n",
140+
"pd.unique(df.name.ravel())"
141+
],
142+
"language": "python",
143+
"metadata": {},
144+
"outputs": [
145+
{
146+
"metadata": {},
147+
"output_type": "pyout",
148+
"prompt_number": 8,
149+
"text": [
150+
"array(['Jason', 'Molly', 'Tina', 'Jake', 'Amy'], dtype=object)"
151+
]
152+
}
153+
],
154+
"prompt_number": 8
155+
}
156+
],
157+
"metadata": {}
158+
}
159+
]
160+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
{
2+
"metadata": {
3+
"name": "",
4+
"signature": "sha256:613378e9ce3af649aba5a1195d8277602953bc48106e8172f34175a26974c784"
5+
},
6+
"nbformat": 3,
7+
"nbformat_minor": 0,
8+
"worksheets": [
9+
{
10+
"cells": [
11+
{
12+
"cell_type": "markdown",
13+
"metadata": {},
14+
"source": [
15+
"# Lower Case Column Names In Pandas Dataframe\n",
16+
"\n",
17+
"This snippit was written by [Chris R. Albon](http://www.chrisralbon.com/) and is part of his collection of [well-documented Python snippits](https://github.com/chrisalbon/code_py). All code is written in Python 3 in iPython notebook and offered under the [Creative Commons Attribution-ShareAlike 4.0 International License](http://creativecommons.org/licenses/by-sa/4.0/\"><img alt=\"Creative Commons License)."
18+
]
19+
},
20+
{
21+
"cell_type": "markdown",
22+
"metadata": {},
23+
"source": [
24+
"## Preliminaries"
25+
]
26+
},
27+
{
28+
"cell_type": "code",
29+
"collapsed": false,
30+
"input": [
31+
"# Import modules\n",
32+
"import pandas as pd\n",
33+
"\n",
34+
"# Set ipython's max row display\n",
35+
"pd.set_option('display.max_row', 1000)\n",
36+
"\n",
37+
"# Set iPython's max column width to 50\n",
38+
"pd.set_option('display.max_columns', 50)"
39+
],
40+
"language": "python",
41+
"metadata": {},
42+
"outputs": [],
43+
"prompt_number": 1
44+
},
45+
{
46+
"cell_type": "markdown",
47+
"metadata": {},
48+
"source": [
49+
"## Create an example dataframe"
50+
]
51+
},
52+
{
53+
"cell_type": "code",
54+
"collapsed": false,
55+
"input": [
56+
"# Create an example dataframe\n",
57+
"data = {'NAME': ['Jason', 'Molly', 'Tina', 'Jake', 'Amy'], \n",
58+
" 'YEAR': [2012, 2012, 2013, 2014, 2014], \n",
59+
" 'REPORTS': [4, 24, 31, 2, 3]}\n",
60+
"df = pd.DataFrame(data, index = ['Cochice', 'Pima', 'Santa Cruz', 'Maricopa', 'Yuma'])\n",
61+
"df"
62+
],
63+
"language": "python",
64+
"metadata": {},
65+
"outputs": [
66+
{
67+
"html": [
68+
"<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
69+
"<table border=\"1\" class=\"dataframe\">\n",
70+
" <thead>\n",
71+
" <tr style=\"text-align: right;\">\n",
72+
" <th></th>\n",
73+
" <th>NAME</th>\n",
74+
" <th>REPORTS</th>\n",
75+
" <th>YEAR</th>\n",
76+
" </tr>\n",
77+
" </thead>\n",
78+
" <tbody>\n",
79+
" <tr>\n",
80+
" <th>Cochice</th>\n",
81+
" <td> Jason</td>\n",
82+
" <td> 4</td>\n",
83+
" <td> 2012</td>\n",
84+
" </tr>\n",
85+
" <tr>\n",
86+
" <th>Pima</th>\n",
87+
" <td> Molly</td>\n",
88+
" <td> 24</td>\n",
89+
" <td> 2012</td>\n",
90+
" </tr>\n",
91+
" <tr>\n",
92+
" <th>Santa Cruz</th>\n",
93+
" <td> Tina</td>\n",
94+
" <td> 31</td>\n",
95+
" <td> 2013</td>\n",
96+
" </tr>\n",
97+
" <tr>\n",
98+
" <th>Maricopa</th>\n",
99+
" <td> Jake</td>\n",
100+
" <td> 2</td>\n",
101+
" <td> 2014</td>\n",
102+
" </tr>\n",
103+
" <tr>\n",
104+
" <th>Yuma</th>\n",
105+
" <td> Amy</td>\n",
106+
" <td> 3</td>\n",
107+
" <td> 2014</td>\n",
108+
" </tr>\n",
109+
" </tbody>\n",
110+
"</table>\n",
111+
"</div>"
112+
],
113+
"metadata": {},
114+
"output_type": "pyout",
115+
"prompt_number": 5,
116+
"text": [
117+
" NAME REPORTS YEAR\n",
118+
"Cochice Jason 4 2012\n",
119+
"Pima Molly 24 2012\n",
120+
"Santa Cruz Tina 31 2013\n",
121+
"Maricopa Jake 2 2014\n",
122+
"Yuma Amy 3 2014"
123+
]
124+
}
125+
],
126+
"prompt_number": 5
127+
},
128+
{
129+
"cell_type": "markdown",
130+
"metadata": {},
131+
"source": [
132+
"## Lowercase column values"
133+
]
134+
},
135+
{
136+
"cell_type": "code",
137+
"collapsed": false,
138+
"input": [
139+
"# Map the lowering function to all column names\n",
140+
"df.columns = map(str.lower, df.columns)"
141+
],
142+
"language": "python",
143+
"metadata": {},
144+
"outputs": [],
145+
"prompt_number": 6
146+
},
147+
{
148+
"cell_type": "code",
149+
"collapsed": false,
150+
"input": [
151+
"df"
152+
],
153+
"language": "python",
154+
"metadata": {},
155+
"outputs": [
156+
{
157+
"html": [
158+
"<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
159+
"<table border=\"1\" class=\"dataframe\">\n",
160+
" <thead>\n",
161+
" <tr style=\"text-align: right;\">\n",
162+
" <th></th>\n",
163+
" <th>name</th>\n",
164+
" <th>reports</th>\n",
165+
" <th>year</th>\n",
166+
" </tr>\n",
167+
" </thead>\n",
168+
" <tbody>\n",
169+
" <tr>\n",
170+
" <th>Cochice</th>\n",
171+
" <td> Jason</td>\n",
172+
" <td> 4</td>\n",
173+
" <td> 2012</td>\n",
174+
" </tr>\n",
175+
" <tr>\n",
176+
" <th>Pima</th>\n",
177+
" <td> Molly</td>\n",
178+
" <td> 24</td>\n",
179+
" <td> 2012</td>\n",
180+
" </tr>\n",
181+
" <tr>\n",
182+
" <th>Santa Cruz</th>\n",
183+
" <td> Tina</td>\n",
184+
" <td> 31</td>\n",
185+
" <td> 2013</td>\n",
186+
" </tr>\n",
187+
" <tr>\n",
188+
" <th>Maricopa</th>\n",
189+
" <td> Jake</td>\n",
190+
" <td> 2</td>\n",
191+
" <td> 2014</td>\n",
192+
" </tr>\n",
193+
" <tr>\n",
194+
" <th>Yuma</th>\n",
195+
" <td> Amy</td>\n",
196+
" <td> 3</td>\n",
197+
" <td> 2014</td>\n",
198+
" </tr>\n",
199+
" </tbody>\n",
200+
"</table>\n",
201+
"</div>"
202+
],
203+
"metadata": {},
204+
"output_type": "pyout",
205+
"prompt_number": 7,
206+
"text": [
207+
" name reports year\n",
208+
"Cochice Jason 4 2012\n",
209+
"Pima Molly 24 2012\n",
210+
"Santa Cruz Tina 31 2013\n",
211+
"Maricopa Jake 2 2014\n",
212+
"Yuma Amy 3 2014"
213+
]
214+
}
215+
],
216+
"prompt_number": 7
217+
}
218+
],
219+
"metadata": {}
220+
}
221+
]
222+
}

0 commit comments

Comments
 (0)