Skip to content

Instantly share code, notes, and snippets.

@pybokeh
Created May 12, 2018 13:34
Show Gist options
  • Save pybokeh/f920eae01c5ccf2d7cb857e4de933fec to your computer and use it in GitHub Desktop.
Save pybokeh/f920eae01c5ccf2d7cb857e4de933fec to your computer and use it in GitHub Desktop.
Untitled.ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "import pandas as pd",
"execution_count": 1,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "df = pd.read_csv('/home/pybokeh/temp/temp.csv')",
"execution_count": 2,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "df",
"execution_count": 3,
"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>id</th>\n <th>value</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td>1466</td>\n <td>24866</td>\n </tr>\n <tr>\n <th>1</th>\n <td>1466</td>\n <td>20375</td>\n </tr>\n <tr>\n <th>2</th>\n <td>1466</td>\n <td>813973</td>\n </tr>\n <tr>\n <th>3</th>\n <td>1466</td>\n <td>16302</td>\n </tr>\n <tr>\n <th>4</th>\n <td>1046</td>\n <td>712</td>\n </tr>\n <tr>\n <th>5</th>\n <td>1046</td>\n <td>783</td>\n </tr>\n <tr>\n <th>6</th>\n <td>1046</td>\n <td>781</td>\n </tr>\n <tr>\n <th>7</th>\n <td>1046</td>\n <td>1953</td>\n </tr>\n <tr>\n <th>8</th>\n <td>1046</td>\n <td>2120</td>\n </tr>\n </tbody>\n</table>\n</div>",
"text/plain": " id value\n0 1466 24866\n1 1466 20375\n2 1466 813973\n3 1466 16302\n4 1046 712\n5 1046 783\n6 1046 781\n7 1046 1953\n8 1046 2120"
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "df.columns",
"execution_count": 4,
"outputs": [
{
"data": {
"text/plain": "Index(['id', 'value'], dtype='object')"
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "id_list = df.id.unique()",
"execution_count": 5,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "id_list",
"execution_count": 6,
"outputs": [
{
"data": {
"text/plain": "array([1466, 1046])"
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "my_dict = {}\nfor id in id_list:\n my_dict[id] = df.query(\"id == @id\")['value'].values",
"execution_count": 7,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "my_dict",
"execution_count": 8,
"outputs": [
{
"data": {
"text/plain": "{1466: array([ 24866, 20375, 813973, 16302]),\n 1046: array([ 712, 783, 781, 1953, 2120])}"
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
]
}
],
"metadata": {
"hide_input": false,
"kernelspec": {
"name": "python3",
"display_name": "Python 3",
"language": "python"
},
"language_info": {
"name": "python",
"version": "3.6.4",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"file_extension": ".py"
},
"varInspector": {
"window_display": false,
"cols": {
"lenName": 16,
"lenType": 16,
"lenVar": 40
},
"kernels_config": {
"python": {
"library": "var_list.py",
"delete_cmd_prefix": "del ",
"delete_cmd_postfix": "",
"varRefreshCmd": "print(var_dic_list())"
},
"r": {
"library": "var_list.r",
"delete_cmd_prefix": "rm(",
"delete_cmd_postfix": ") ",
"varRefreshCmd": "cat(var_dic_list()) "
}
},
"types_to_exclude": [
"module",
"function",
"builtin_function_or_method",
"instance",
"_Feature"
]
},
"gist": {
"id": "",
"data": {
"description": "Untitled.ipynb",
"public": true
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment