mirror of
https://github.com/donnemartin/data-science-ipython-notebooks.git
synced 2024-03-22 13:30:56 +08:00
Added snippet to drop a column in a DataFrame. Renamed pop to population to avoid clashing with the DataFrame pop function.
This commit is contained in:
parent
f31a289eab
commit
d6348012a4
|
@ -1,7 +1,7 @@
|
|||
{
|
||||
"metadata": {
|
||||
"name": "",
|
||||
"signature": "sha256:1c8b7cab9b55eb5888612d0b5149649565c258456e73e61b039225439aa11502"
|
||||
"signature": "sha256:b619f1fd1f2d4495d6a2fe9d048c09b7319b119d4e10a5b2348f0ac6f380a27c"
|
||||
},
|
||||
"nbformat": 3,
|
||||
"nbformat_minor": 0,
|
||||
|
@ -45,7 +45,7 @@
|
|||
"input": [
|
||||
"data_1 = {'state' : ['VA', 'VA', 'VA', 'MD', 'MD'],\n",
|
||||
" 'year' : [2012, 2013, 2014, 2014, 2015],\n",
|
||||
" 'pop' : [5.0, 5.1, 5.2, 4.0, 4.1]}\n",
|
||||
" 'population' : [5.0, 5.1, 5.2, 4.0, 4.1]}\n",
|
||||
"df_1 = DataFrame(data_1)\n",
|
||||
"df_1"
|
||||
],
|
||||
|
@ -59,7 +59,7 @@
|
|||
" <thead>\n",
|
||||
" <tr style=\"text-align: right;\">\n",
|
||||
" <th></th>\n",
|
||||
" <th>pop</th>\n",
|
||||
" <th>population</th>\n",
|
||||
" <th>state</th>\n",
|
||||
" <th>year</th>\n",
|
||||
" </tr>\n",
|
||||
|
@ -103,7 +103,7 @@
|
|||
"output_type": "pyout",
|
||||
"prompt_number": 2,
|
||||
"text": [
|
||||
" pop state year\n",
|
||||
" population state year\n",
|
||||
"0 5.0 VA 2012\n",
|
||||
"1 5.1 VA 2013\n",
|
||||
"2 5.2 VA 2014\n",
|
||||
|
@ -130,7 +130,7 @@
|
|||
" <thead>\n",
|
||||
" <tr style=\"text-align: right;\">\n",
|
||||
" <th></th>\n",
|
||||
" <th>pop</th>\n",
|
||||
" <th>population</th>\n",
|
||||
" <th>state</th>\n",
|
||||
" <th>year</th>\n",
|
||||
" </tr>\n",
|
||||
|
@ -162,7 +162,7 @@
|
|||
"output_type": "pyout",
|
||||
"prompt_number": 3,
|
||||
"text": [
|
||||
" pop state year\n",
|
||||
" population state year\n",
|
||||
"0 5.0 VA 2012\n",
|
||||
"1 5.1 VA 2013\n",
|
||||
"2 5.2 VA 2014"
|
||||
|
@ -195,7 +195,7 @@
|
|||
" <thead>\n",
|
||||
" <tr style=\"text-align: right;\">\n",
|
||||
" <th></th>\n",
|
||||
" <th>pop</th>\n",
|
||||
" <th>population</th>\n",
|
||||
" <th>state</th>\n",
|
||||
" <th>year</th>\n",
|
||||
" </tr>\n",
|
||||
|
@ -239,7 +239,7 @@
|
|||
"output_type": "pyout",
|
||||
"prompt_number": 4,
|
||||
"text": [
|
||||
" pop state year\n",
|
||||
" population state year\n",
|
||||
"0 5.0 VIRGINIA 2012\n",
|
||||
"1 5.1 VIRGINIA 2013\n",
|
||||
"2 5.2 VIRGINIA 2014\n",
|
||||
|
@ -274,7 +274,7 @@
|
|||
" <thead>\n",
|
||||
" <tr style=\"text-align: right;\">\n",
|
||||
" <th></th>\n",
|
||||
" <th>pop</th>\n",
|
||||
" <th>population</th>\n",
|
||||
" <th>state</th>\n",
|
||||
" <th>year</th>\n",
|
||||
" </tr>\n",
|
||||
|
@ -318,7 +318,7 @@
|
|||
"output_type": "pyout",
|
||||
"prompt_number": 5,
|
||||
"text": [
|
||||
" pop state year\n",
|
||||
" population state year\n",
|
||||
"0 5.0 VIRGINIA 2012\n",
|
||||
"1 5.1 VIRGINIA 2013\n",
|
||||
"2 5.2 VIRGINIA 2014\n",
|
||||
|
@ -333,17 +333,14 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Concatenate two DataFrames:"
|
||||
"Drop the 'population' column and return a copy of the DataFrame:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"collapsed": false,
|
||||
"input": [
|
||||
"data_2 = {'state' : ['NY', 'NY', 'NY', 'FL', 'FL'],\n",
|
||||
" 'year' : [2012, 2013, 2014, 2014, 2015],\n",
|
||||
" 'pop' : [6.0, 6.1, 6.2, 3.0, 3.1]}\n",
|
||||
"df_2 = DataFrame(data_2)\n",
|
||||
"df_2 = df_1.drop('population', axis=1)\n",
|
||||
"df_2"
|
||||
],
|
||||
"language": "python",
|
||||
|
@ -356,7 +353,83 @@
|
|||
" <thead>\n",
|
||||
" <tr style=\"text-align: right;\">\n",
|
||||
" <th></th>\n",
|
||||
" <th>pop</th>\n",
|
||||
" <th>state</th>\n",
|
||||
" <th>year</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td> VIRGINIA</td>\n",
|
||||
" <td> 2012</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td> VIRGINIA</td>\n",
|
||||
" <td> 2013</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td> VIRGINIA</td>\n",
|
||||
" <td> 2014</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td> MARYLAND</td>\n",
|
||||
" <td> 2014</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td> MARYLAND</td>\n",
|
||||
" <td> 2015</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"metadata": {},
|
||||
"output_type": "pyout",
|
||||
"prompt_number": 6,
|
||||
"text": [
|
||||
" state year\n",
|
||||
"0 VIRGINIA 2012\n",
|
||||
"1 VIRGINIA 2013\n",
|
||||
"2 VIRGINIA 2014\n",
|
||||
"3 MARYLAND 2014\n",
|
||||
"4 MARYLAND 2015"
|
||||
]
|
||||
}
|
||||
],
|
||||
"prompt_number": 6
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Concatenate two DataFrames:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"collapsed": false,
|
||||
"input": [
|
||||
"data_2 = {'state' : ['NY', 'NY', 'NY', 'FL', 'FL'],\n",
|
||||
" 'year' : [2012, 2013, 2014, 2014, 2015],\n",
|
||||
" 'population' : [6.0, 6.1, 6.2, 3.0, 3.1]}\n",
|
||||
"df_3 = DataFrame(data_2)\n",
|
||||
"df_3"
|
||||
],
|
||||
"language": "python",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"html": [
|
||||
"<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
|
||||
"<table border=\"1\" class=\"dataframe\">\n",
|
||||
" <thead>\n",
|
||||
" <tr style=\"text-align: right;\">\n",
|
||||
" <th></th>\n",
|
||||
" <th>population</th>\n",
|
||||
" <th>state</th>\n",
|
||||
" <th>year</th>\n",
|
||||
" </tr>\n",
|
||||
|
@ -398,9 +471,9 @@
|
|||
],
|
||||
"metadata": {},
|
||||
"output_type": "pyout",
|
||||
"prompt_number": 6,
|
||||
"prompt_number": 7,
|
||||
"text": [
|
||||
" pop state year\n",
|
||||
" population state year\n",
|
||||
"0 6.0 NY 2012\n",
|
||||
"1 6.1 NY 2013\n",
|
||||
"2 6.2 NY 2014\n",
|
||||
|
@ -409,14 +482,14 @@
|
|||
]
|
||||
}
|
||||
],
|
||||
"prompt_number": 6
|
||||
"prompt_number": 7
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"collapsed": false,
|
||||
"input": [
|
||||
"df_3 = pd.concat([df_1, df_2])\n",
|
||||
"df_3"
|
||||
"df_4 = pd.concat([df_1, df_3])\n",
|
||||
"df_4"
|
||||
],
|
||||
"language": "python",
|
||||
"metadata": {},
|
||||
|
@ -428,7 +501,7 @@
|
|||
" <thead>\n",
|
||||
" <tr style=\"text-align: right;\">\n",
|
||||
" <th></th>\n",
|
||||
" <th>pop</th>\n",
|
||||
" <th>population</th>\n",
|
||||
" <th>state</th>\n",
|
||||
" <th>year</th>\n",
|
||||
" </tr>\n",
|
||||
|
@ -500,9 +573,9 @@
|
|||
],
|
||||
"metadata": {},
|
||||
"output_type": "pyout",
|
||||
"prompt_number": 7,
|
||||
"prompt_number": 8,
|
||||
"text": [
|
||||
" pop state year\n",
|
||||
" population state year\n",
|
||||
"0 5.0 VIRGINIA 2012\n",
|
||||
"1 5.1 VIRGINIA 2013\n",
|
||||
"2 5.2 VIRGINIA 2014\n",
|
||||
|
@ -516,7 +589,7 @@
|
|||
]
|
||||
}
|
||||
],
|
||||
"prompt_number": 7
|
||||
"prompt_number": 8
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
|
@ -525,7 +598,7 @@
|
|||
"language": "python",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"prompt_number": 7
|
||||
"prompt_number": 8
|
||||
}
|
||||
],
|
||||
"metadata": {}
|
||||
|
|
Loading…
Reference in New Issue
Block a user