Skip to content

Commit

Permalink
Fix errors in Pandas notebook (#194)
Browse files Browse the repository at this point in the history
  • Loading branch information
edoardob90 authored Feb 23, 2024
1 parent 4a478af commit 1b42590
Showing 1 changed file with 21 additions and 37 deletions.
58 changes: 21 additions & 37 deletions library_pandas.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1003,7 +1003,7 @@
"metadata": {},
"outputs": [],
"source": [
"!wc -l data/earthquakes.csv"
"!wc -l data/01/earthquakes.csv"
]
},
{
Expand All @@ -1014,7 +1014,7 @@
"**Windows users**: if the above doesn't work for you (depends on your setup), then use this instead:\n",
"\n",
"```python\n",
"!find /c /v \"\" data\\earthquakes.csv\n",
"!find /c /v \"\" data\\01\\earthquakes.csv\n",
"```\n",
"\n",
"\n",
Expand All @@ -1030,7 +1030,7 @@
"metadata": {},
"outputs": [],
"source": [
"!ls -lh data | grep earthquakes.csv"
"!ls -lh data/01 | grep earthquakes.csv"
]
},
{
Expand All @@ -1041,7 +1041,7 @@
"**Windows users**: if the above doesn't work for you (depends on your setup), then use this instead:\n",
"\n",
"```python\n",
"!dir data | findstr \"earthquakes.csv\"\n",
"!dir data\\01 | findstr \"earthquakes.csv\"\n",
"```\n",
"\n",
"We can even capture the result of a command and use it in our Python code:"
Expand All @@ -1054,7 +1054,7 @@
"metadata": {},
"outputs": [],
"source": [
"files = !ls -lh data\n",
"files = !ls -lh data/01\n",
"[file for file in files if 'earthquake' in file]"
]
},
Expand All @@ -1066,7 +1066,7 @@
"**Windows users**: if the above doesn't work for you (depends on your setup), then use this instead:\n",
"\n",
"```python\n",
"files = !dir data\n",
"files = !dir data\\01\n",
"[file for file in files if 'earthquake' in file]\n",
"```"
]
Expand All @@ -1088,7 +1088,7 @@
"metadata": {},
"outputs": [],
"source": [
"!head -n 2 data/earthquakes.csv"
"!head -n 2 data/01/earthquakes.csv"
]
},
{
Expand All @@ -1100,7 +1100,7 @@
"\n",
"```python\n",
"n = 2\n",
"with open('data/earthquakes.csv', 'r') as file:\n",
"with open('data/01/earthquakes.csv', 'r') as file:\n",
" for _ in range(n):\n",
" print(file.readline(), end='\\r')\n",
"```\n",
Expand All @@ -1116,7 +1116,7 @@
"metadata": {},
"outputs": [],
"source": [
"!tail -n 1 data/earthquakes.csv"
"!tail -n 1 data/01/earthquakes.csv"
]
},
{
Expand All @@ -1129,7 +1129,7 @@
"```python\n",
"import os\n",
"\n",
"with open('data/earthquakes.csv', 'rb') as file:\n",
"with open('data/01/earthquakes.csv', 'rb') as file:\n",
" file.seek(0, os.SEEK_END)\n",
" while file.read(1) != b'\\n':\n",
" file.seek(-2, os.SEEK_CUR)\n",
Expand All @@ -1140,7 +1140,7 @@
"\n",
"```python\n",
"n = 2\n",
"with open('data/earthquakes.csv', 'r') as file:\n",
"with open('data/01/earthquakes.csv', 'r') as file:\n",
" print('\\r'.join(file.readlines()[-n:]))\n",
"```\n",
"\n"
Expand All @@ -1164,7 +1164,7 @@
"metadata": {},
"outputs": [],
"source": [
"!awk -F',' '{print NF; exit}' data/earthquakes.csv"
"!awk -F',' '{print NF; exit}' data/01/earthquakes.csv"
]
},
{
Expand All @@ -1175,7 +1175,7 @@
"**Windows users**: if the above or below don't work for you (depends on your setup), then use this instead:\n",
"\n",
"```python\n",
"with open('data/earthquakes.csv', 'r') as file:\n",
"with open('data/01/earthquakes.csv', 'r') as file:\n",
" print(len(file.readline().split(',')))\n",
"```\n",
"\n",
Expand All @@ -1190,7 +1190,7 @@
"metadata": {},
"outputs": [],
"source": [
"headers = !head -n 1 data/earthquakes.csv\n",
"headers = !head -n 1 data/01/earthquakes.csv\n",
"len(headers[0].split(','))"
]
},
Expand Down Expand Up @@ -1220,7 +1220,7 @@
"metadata": {},
"outputs": [],
"source": [
"df = pd.read_csv('data/earthquakes.csv')"
"df = pd.read_csv('data/01/earthquakes.csv')"
]
},
{
Expand Down Expand Up @@ -2155,24 +2155,6 @@
"pd.concat([tsunami, no_tsunami]).shape"
]
},
{
"cell_type": "markdown",
"id": "d38495fa-fe5e-4937-9774-b90c0d26e6d9",
"metadata": {},
"source": [
"Note that the previous result is equivalent to running the `append()` method of the dataframe:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8c6be158-f310-42b7-a05e-cd4b6a6e07b2",
"metadata": {},
"outputs": [],
"source": [
"tsunami.append(no_tsunami).shape"
]
},
{
"cell_type": "markdown",
"id": "ba559768-6848-4eca-9b84-04b8b6e78417",
Expand All @@ -2189,7 +2171,7 @@
"outputs": [],
"source": [
"additional_columns = pd.read_csv(\n",
" 'data/earthquakes.csv', usecols=['tz', 'felt', 'ids']\n",
" 'data/01/earthquakes.csv', usecols=['tz', 'felt', 'ids']\n",
")\n",
"pd.concat([df.head(2), additional_columns.head(2)], axis=1)"
]
Expand All @@ -2210,7 +2192,7 @@
"outputs": [],
"source": [
"additional_columns = pd.read_csv(\n",
" 'data/earthquakes.csv', usecols=['tz', 'felt', 'ids', 'time'], index_col='time'\n",
" 'data/01/earthquakes.csv', usecols=['tz', 'felt', 'ids', 'time'], index_col='time'\n",
")\n",
"pd.concat([df.head(2), additional_columns.head(2)], axis=1)"
]
Expand Down Expand Up @@ -3618,14 +3600,16 @@
"metadata": {},
"outputs": [],
"source": [
"extra_data = long_df.append([{\n",
"extra_data = pd.DataFrame([{\n",
" 'datatype': 'TAVG', \n",
" 'date': '2018-10-01', \n",
" 'temp_C': 10, \n",
" 'temp_F': 50\n",
"}]).set_index(['date', 'datatype']).sort_index()\n",
"\n",
"extra_data['2018-10-01':'2018-10-02']"
"extra_data = pd.concat([long_df, extra_data])\n",
"\n",
"extra_data.head()"
]
},
{
Expand Down

0 comments on commit 1b42590

Please sign in to comment.