From 4450c119ad0cc31bdaf5b4229085a2b2af42ddfc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= Date: Wed, 1 Feb 2023 02:43:18 +0100 Subject: [PATCH] Pandas --- README.md | 26 +++++++++++++------------- index.html | 26 +++++++++++++------------- 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 370c6ab..a34b35f 100644 --- a/README.md +++ b/README.md @@ -3120,7 +3120,6 @@ Pandas ```python # $ pip3 install pandas matplotlib import pandas as pd -from pandas import Series, DataFrame import matplotlib.pyplot as plt ``` @@ -3128,16 +3127,16 @@ import matplotlib.pyplot as plt **Ordered dictionary with a name.** ```python ->>> Series([1, 2], index=['x', 'y'], name='a') +>>> pd.Series([1, 2], index=['x', 'y'], name='a') x 1 y 2 Name: a, dtype: int64 ``` ```python - = Series() # Assigns RangeIndex starting at 0. - = Series() # Takes dictionary's keys for index. - = Series(, index=) # Only keeps items with keys specified in index. + = pd.Series() # Assigns RangeIndex starting at 0. + = pd.Series() # Takes dictionary's keys for index. + = pd.Series(, index=) # Only keeps items with keys specified in index. ``` ```python @@ -3176,7 +3175,7 @@ plt.show() # Displays the plot. Also plt.sav ``` ```python ->>> sr = Series([1, 2], index=['x', 'y']) +>>> sr = pd.Series([1, 2], index=['x', 'y']) x 1 y 2 ``` @@ -3201,20 +3200,21 @@ y 2 ``` * **Methods ffill(), interpolate() and fillna() accept argument 'inplace' that defaults to False.** * **Last result has a hierarchical index. Use `'[key_1, key_2]'` to get its values.** +* **Keys, indexes and bools can't be tuples because `'obj[x, y]'` becomes `'obj[(x, y)]'`.** ### DataFrame **Table with labeled rows and columns.** ```python ->>> DataFrame([[1, 2], [3, 4]], index=['a', 'b'], columns=['x', 'y']) +>>> pd.DataFrame([[1, 2], [3, 4]], index=['a', 'b'], columns=['x', 'y']) x y a 1 2 b 3 4 ``` ```python - = DataFrame() # Rows can be either lists, dicts or series. - = DataFrame() # Columns can be either lists, dicts or series. + = pd.DataFrame() # Rows can be either lists, dicts or series. + = pd.DataFrame() # Columns can be either lists, dicts or series. ``` ```python @@ -3244,11 +3244,11 @@ b 3 4 #### DataFrame — Merge, Join, Concat: ```python ->>> l = DataFrame([[1, 2], [3, 4]], index=['a', 'b'], columns=['x', 'y']) +>>> l = pd.DataFrame([[1, 2], [3, 4]], index=['a', 'b'], columns=['x', 'y']) x y a 1 2 b 3 4 ->>> r = DataFrame([[4, 5], [6, 7]], index=['b', 'c'], columns=['y', 'z']) +>>> r = pd.DataFrame([[4, 5], [6, 7]], index=['b', 'c'], columns=['y', 'z']) y z b 4 5 c 6 7 @@ -3295,7 +3295,7 @@ c 6 7 * **All operations operate on columns by default. Pass `'axis=1'` to process the rows instead.** ```python ->>> df = DataFrame([[1, 2], [3, 4]], index=['a', 'b'], columns=['x', 'y']) +>>> df = pd.DataFrame([[1, 2], [3, 4]], index=['a', 'b'], columns=['x', 'y']) x y a 1 2 b 3 4 @@ -3347,7 +3347,7 @@ plt.show() # Displays the plot. Also plt.sav **Object that groups together rows of a dataframe based on the value of the passed column.** ```python ->>> df = DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 6]], index=list('abc'), columns=list('xyz')) +>>> df = pd.DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 6]], list('abc'), list('xyz')) >>> df.groupby('z').get_group(6) x y b 4 5 diff --git a/index.html b/index.html index bfa8f7d..6d7a07d 100644 --- a/index.html +++ b/index.html @@ -2555,20 +2555,19 @@ W, H, MAX_S = 50, 50<

#Pandas

# $ pip3 install pandas matplotlib
 import pandas as pd
-from pandas import Series, DataFrame
 import matplotlib.pyplot as plt
 
-

Series

Ordered dictionary with a name.

>>> Series([1, 2], index=['x', 'y'], name='a')
+

Series

Ordered dictionary with a name.

>>> pd.Series([1, 2], index=['x', 'y'], name='a')
 x    1
 y    2
 Name: a, dtype: int64
 
-
<Sr> = Series(<list>)                          # Assigns RangeIndex starting at 0.
-<Sr> = Series(<dict>)                          # Takes dictionary's keys for index.
-<Sr> = Series(<dict/Series>, index=<list>)     # Only keeps items with keys specified in index.
+
<Sr> = pd.Series(<list>)                       # Assigns RangeIndex starting at 0.
+<Sr> = pd.Series(<dict>)                       # Takes dictionary's keys for index.
+<Sr> = pd.Series(<dict/Series>, index=<list>)  # Only keeps items with keys specified in index.
 
<el> = <Sr>.loc[key]                           # Or: <Sr>.iloc[index]
 <Sr> = <Sr>.loc[keys]                          # Or: <Sr>.iloc[indexes]
@@ -2593,7 +2592,7 @@ plt.show()                                     # Disp
 <Sr> = <Sr>.fillna(<el>)                       # Or: <Sr>.agg/transform/map(lambda <el>: <el>)
 
-
>>> sr = Series([1, 2], index=['x', 'y'])
+
>>> sr = pd.Series([1, 2], index=['x', 'y'])
 x    1
 y    2
 
@@ -2616,16 +2615,17 @@ y 2
  • Methods ffill(), interpolate() and fillna() accept argument 'inplace' that defaults to False.
  • Last result has a hierarchical index. Use '<Sr>[key_1, key_2]' to get its values.
  • +
  • Keys, indexes and bools can't be tuples because 'obj[x, y]' becomes 'obj[(x, y)]'.
-

DataFrame

Table with labeled rows and columns.

>>> DataFrame([[1, 2], [3, 4]], index=['a', 'b'], columns=['x', 'y'])
+

DataFrame

Table with labeled rows and columns.

>>> pd.DataFrame([[1, 2], [3, 4]], index=['a', 'b'], columns=['x', 'y'])
    x  y
 a  1  2
 b  3  4
 
-
<DF>    = DataFrame(<list_of_rows>)            # Rows can be either lists, dicts or series.
-<DF>    = DataFrame(<dict_of_columns>)         # Columns can be either lists, dicts or series.
+
<DF>    = pd.DataFrame(<list_of_rows>)         # Rows can be either lists, dicts or series.
+<DF>    = pd.DataFrame(<dict_of_columns>)      # Columns can be either lists, dicts or series.
 
<el>    = <DF>.loc[row_key, column_key]        # Or: <DF>.iloc[row_index, column_index]
 <Sr/DF> = <DF>.loc[row_key/s]                  # Or: <DF>.iloc[row_index/es]
@@ -2644,11 +2644,11 @@ b  3  4
 <DF>    = <DF>.sort_index(ascending=True)      # Sorts rows by row keys. Use `axis=1` for cols.
 <DF>    = <DF>.sort_values(column_key/s)       # Sorts rows by the passed column/s. Same.
 
-

DataFrame — Merge, Join, Concat:

>>> l = DataFrame([[1, 2], [3, 4]], index=['a', 'b'], columns=['x', 'y'])
+

DataFrame — Merge, Join, Concat:

>>> l = pd.DataFrame([[1, 2], [3, 4]], index=['a', 'b'], columns=['x', 'y'])
    x  y
 a  1  2
 b  3  4
->>> r = DataFrame([[4, 5], [6, 7]], index=['b', 'c'], columns=['y', 'z'])
+>>> r = pd.DataFrame([[4, 5], [6, 7]], index=['b', 'c'], columns=['y', 'z'])
    y  z
 b  4  5
 c  6  7
@@ -2692,7 +2692,7 @@ c  6  7
 
  • All operations operate on columns by default. Pass 'axis=1' to process the rows instead.
-
>>> df = DataFrame([[1, 2], [3, 4]], index=['a', 'b'], columns=['x', 'y'])
+
>>> df = pd.DataFrame([[1, 2], [3, 4]], index=['a', 'b'], columns=['x', 'y'])
    x  y
 a  1  2
 b  3  4
@@ -2732,7 +2732,7 @@ plt.show()                                     # Disp
 <DF>.to_pickle/excel(<path>)                   # Run `$ pip3 install openpyxl` for xlsx files.
 <DF>.to_sql('<table_name>', <connection>)      # Accepts SQLite3 or SQLAlchemy connection.
 
-

GroupBy

Object that groups together rows of a dataframe based on the value of the passed column.

>>> df = DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 6]], index=list('abc'), columns=list('xyz'))
+

GroupBy

Object that groups together rows of a dataframe based on the value of the passed column.

>>> df = pd.DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 6]], list('abc'), list('xyz'))
 >>> df.groupby('z').get_group(6)
    x  y
 b  4  5