Skip to content

Instantly share code, notes, and snippets.

@leliel12
Created November 29, 2021 18:30
Show Gist options
  • Select an option

  • Save leliel12/4895d80290a855fb0b03d49de884c15b to your computer and use it in GitHub Desktop.

Select an option

Save leliel12/4895d80290a855fb0b03d49de884c15b to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"id": "750e6c0b-2d31-4120-be30-e206e69f8ca5",
"metadata": {},
"outputs": [],
"source": [
"code = \"\"\"a,b,c,d\n",
"1,2,3,4\n",
"5,6,7,8\n",
"\"\"\"\n",
"\n",
"with open(\"data.csv\", \"w\") as fp:\n",
" fp.write(code)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "c528a947-9654-4459-8add-d5ead2a8a344",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "df0b92e4-22cd-4dcc-8597-3e1b3f32c724",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\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",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>a</th>\n",
" <th>b</th>\n",
" <th>c</th>\n",
" <th>d</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1</td>\n",
" <td>2</td>\n",
" <td>3</td>\n",
" <td>4</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>5</td>\n",
" <td>6</td>\n",
" <td>7</td>\n",
" <td>8</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" a b c d\n",
"0 1 2 3 4\n",
"1 5 6 7 8"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.read_csv(\"data.csv\")"
]
},
{
"cell_type": "code",
"execution_count": 32,
"id": "2eca28fa-9793-4ec2-ac49-c6df04076719",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\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",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>a</th>\n",
" <th>b</th>\n",
" <th>c</th>\n",
" <th>d</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1</td>\n",
" <td>2</td>\n",
" <td>3</td>\n",
" <td>4</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>5</td>\n",
" <td>6</td>\n",
" <td>7</td>\n",
" <td>8</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div><br><b>Esto no es un df</b>"
],
"text/plain": [
" a b c d\n",
"0 1 2 3 4\n",
"1 5 6 7 8"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import attr\n",
"\n",
"@attr.s(cmp=False, repr=False)\n",
"class Cell:\n",
" _df = attr.ib()\n",
" \n",
" @classmethod\n",
" def from_csv(cls, path):\n",
" return cls(df=pd.read_csv(path))\n",
" \n",
" @classmethod\n",
" def from_excel(cls, path):\n",
" return cls(df=pd.read_excel(path))\n",
" \n",
" def __repr__(self):\n",
" return repr(self._df)\n",
" \n",
" def _repr_html_(self):\n",
" return f\"{self._df._repr_html_()}<br><b>Esto no es un df</b>\"\n",
" \n",
" def __getitem__(self, slice):\n",
" sliced = self._df.__getitem__(slice)\n",
" return Cell(df=sliced)\n",
" \n",
" def __eq__(self, other):\n",
" return self._df == other\n",
" \n",
" def __ne__(self, other):\n",
" return not self == other\n",
" \n",
" def get_dataframe(self):\n",
" return self._df.copy()\n",
" \n",
" \n",
"Cell.from_csv(\"data.csv\")"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "c3729222-3124-49f9-a652-31e99d2b2685",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0 True\n",
"1 False\n",
"Name: a, dtype: bool"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cell[\"a\"]== 1"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0580ed07-acd0-49cb-b382-8ab108d359c7",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.9.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment