In [2]:
%pip install pandas
Requirement already satisfied: pandas in /Users/olivine/python_projects/.venv/lib/python3.13/site-packages (3.0.5)
Requirement already satisfied: numpy>=1.26.0 in /Users/olivine/python_projects/.venv/lib/python3.13/site-packages (from pandas) (2.5.0)
Requirement already satisfied: python-dateutil>=2.8.2 in /Users/olivine/python_projects/.venv/lib/python3.13/site-packages (from pandas) (2.9.0.post0)
Requirement already satisfied: six>=1.5 in /Users/olivine/python_projects/.venv/lib/python3.13/site-packages (from python-dateutil>=2.8.2->pandas) (1.17.0)
Note: you may need to restart the kernel to use updated packages.
In [3]:
import pandas as pd
import numpy as np
In [4]:
data = [[1, 'Joe', 70000, 3], [2, 'Henry', 80000, 4], [3, 'Sam', 60000, None], [4, 'Max', 90000, None]]
employee = pd.DataFrame(data, columns=['id', 'name', 'salary', 'managerId']).astype({'id':'Int64', 'name':'object', 'salary':'Int64', 'managerId':'Int64'})
employee
Out[4]:
id name salary managerId
0 1 Joe 70000 3
1 2 Henry 80000 4
2 3 Sam 60000 <NA>
3 4 Max 90000 <NA>
In [5]:
salary_map = employee.set_index("id")["salary"]
salary_map
Out[5]:
id
1    70000
2    80000
3    60000
4    90000
Name: salary, dtype: Int64
In [6]:
employee["manager_salary"]=employee["managerId"].map(salary_map)
In [7]:
employee
Out[7]:
id name salary managerId manager_salary
0 1 Joe 70000 3 60000
1 2 Henry 80000 4 90000
2 3 Sam 60000 <NA> <NA>
3 4 Max 90000 <NA> <NA>
In [8]:
employee.query('salary > manager_salary')
Out[8]:
id name salary managerId manager_salary
0 1 Joe 70000 3 60000
In [9]:
data = [[1, 'a@b.com'], [2, 'c@d.com'], [3, 'a@b.com']]
person = pd.DataFrame(data, columns=['id', 'email']).astype({'id':'Int64', 'email':'object'})


person.describe()
Out[9]:
id
count 3.0
mean 2.0
std 1.0
min 1.0
25% 1.5
50% 2.0
75% 2.5
max 3.0
In [10]:
grouped = person.groupby('email')
person
Out[10]:
id email
0 1 a@b.com
1 2 c@d.com
2 3 a@b.com
In [11]:
duplicate_groups = grouped.filter(lambda group: len(group) > 1)
duplicate_groups
Out[11]:
id email
0 1 a@b.com
2 3 a@b.com
In [12]:
duplicate_emails = duplicate_groups["email"]

duplicate_emails
Out[12]:
0    a@b.com
2    a@b.com
Name: email, dtype: object
In [13]:
duplicate_emails.drop_duplicates()
Out[13]:
0    a@b.com
Name: email, dtype: object
In [14]:
data = [[1, 100], [2, 200], [3, 300], [4, 100], [6, 200]]
employee = pd.DataFrame(data, columns=['id', 'salary']).astype({'id':'int64', 'salary':'int64'})
In [15]:
singles = employee['salary'].drop_duplicates()
In [16]:
singles
Out[16]:
0    100
1    200
2    300
Name: salary, dtype: int64
In [17]:
salary = singles.nlargest(2).iloc[-1] if len(singles) > 1 else None
In [18]:
salary
Out[18]:
np.int64(200)
In [19]:
out = pd.DataFrame({"SecondHighestSalary":[salary]})
In [20]:
data1 = pd.DataFrame(np.arange(36).reshape(9,4), columns = ['r', 'b', 'g', 'y'], index = ['one', 'two', 'three']*3)
data1
Out[20]:
r b g y
one 0 1 2 3
two 4 5 6 7
three 8 9 10 11
one 12 13 14 15
two 16 17 18 19
three 20 21 22 23
one 24 25 26 27
two 28 29 30 31
three 32 33 34 35
In [21]:
data1.iloc[4, 1]==data1.iloc[4, 2]
Out[21]:
np.False_
In [22]:
data1['y'].nlargest(20)
Out[22]:
three    35
two      31
one      27
three    23
two      19
one      15
three    11
two       7
one       3
Name: y, dtype: int64
In [23]:
l = range(5)
l[:10]
Out[23]:
range(0, 5)
In [24]:
l=[1, 2, 4]
mid = l[len(l)//2 - 1 : len(l)//2 + 2]
mid
Out[24]:
[1, 2, 4]
In [25]:
len(l)
Out[25]:
3
In [26]:
stack = (0,1,2,3)
stack[0]
Out[26]:
0
In [27]:
out=[[0 for _ in range(5)] for _ in range(3)]
In [28]:
out
Out[28]:
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
In [29]:
out[0][0]=7
In [30]:
out
Out[30]:
[[7, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
In [31]:
matrix = 
stack = [(0,0)]
#stores tuples (w, row) where row is smallest with streak of width w
row=0
for i in range(len(matrix)):
    # iterate over rows
    if streak[i][j] > stack[-1][0]:
        stack.append((streak[i][j], i))
    else:
        #test previous rectangles and remove streaks wider than current
        while streak[i][j] <= stack[-1][0]:
            (w,row) = stack.pop()
            if (i-row)*w > out:
                out = (i-row)*w
            if streak[i][j] == w:
                break
        stack.append((streak[i][j], row))


    print(stack, out)
  Cell In[31], line 1
    matrix =
             ^
SyntaxError: invalid syntax
In [ ]:
pd.Series.add_suffix?
In [34]:
def mult(x: int, y:int)->int:
    return x*y
mult(10.2, 4)
Out[34]:
40.8
In [44]:
data = [[1, 1], [2, 1], [3, 1], [4, 2], [5, 1], [6, 2], [7, 2]]
logs = pd.DataFrame(data, columns=['id', 'num']).astype({'id':'Int64', 'num':'Int64'})
logs['next'] = logs['num'].shift(1)
logs['2next'] = logs['num'].shift(2)
logs
Out[44]:
id num next 2next
0 1 1 <NA> <NA>
1 2 1 1 <NA>
2 3 1 1 1
3 4 2 1 1
4 5 1 2 1
5 6 2 1 2
6 7 2 2 1
In [52]:
logs[(logs['num']==logs['next']) & (logs['num']==logs['2next'])]
Out[52]:
id num next 2next
2 3 1 1 1
In [51]:
(logs['num']==logs['next']) & (logs['num']==logs['2next'])
Out[51]:
0     <NA>
1     <NA>
2     True
3    False
4    False
5    False
6    False
dtype: boolean
In [49]:
pd.Series.any?
Signature:
pd.Series.any(
    self,
    *,
    axis: 'Axis' = 0,
    bool_only: 'bool' = False,
    skipna: 'bool' = True,
    **kwargs,
) -> 'bool'
Docstring:
Return whether any element is True, potentially over an axis.

Returns False unless there is at least one element within a series or
along a Dataframe axis that is True or equivalent (e.g. non-zero or
non-empty).

Parameters
----------
axis : {0 or 'index', 1 or 'columns', None}, default 0
    Indicate which axis or axes should be reduced. For `Series` this parameter
    is unused and defaults to 0.

    * 0 / 'index' : reduce the index, return a Series whose index is the
      original column labels.
    * 1 / 'columns' : reduce the columns, return a Series whose index is the
      original index.
    * None : reduce all axes, return a scalar.

bool_only : bool, default False
    Include only boolean columns. Not implemented for Series.
skipna : bool, default True
    Exclude NA/null values. If the entire row/column is NA and skipna is
    True, then the result will be False, as for an empty row/column.
    If skipna is False, then NA are treated as True, because these are not
    equal to zero.
**kwargs : any, default None
    Additional keywords have no effect but might be accepted for
    compatibility with NumPy.

Returns
-------
Series or scalar
    If axis=None, then a scalar boolean is returned.
    Otherwise a Series is returned with index matching the index argument.

See Also
--------
numpy.any : Numpy version of this method.
Series.any : Return whether any element is True.
Series.all : Return whether all elements are True.
DataFrame.any : Return whether any element is True over requested axis.
DataFrame.all : Return whether all elements are True over requested axis.

Examples
--------
**Series**

For Series input, the output is a scalar indicating whether any element
is True.

>>> pd.Series([False, False]).any()
False
>>> pd.Series([True, False]).any()
True
>>> pd.Series([], dtype="float64").any()
False
>>> pd.Series([np.nan]).any()
False
>>> pd.Series([np.nan]).any(skipna=False)
True

**DataFrame**

Whether each column contains at least one True element (the default).

>>> df = pd.DataFrame({"A": [1, 2], "B": [0, 2], "C": [0, 0]})
>>> df
   A  B  C
0  1  0  0
1  2  2  0

>>> df.any()
A     True
B     True
C    False
dtype: bool

Aggregating over the columns.

>>> df = pd.DataFrame({"A": [True, False], "B": [1, 2]})
>>> df
       A  B
0   True  1
1  False  2

>>> df.any(axis="columns")
0    True
1    True
dtype: bool

>>> df = pd.DataFrame({"A": [True, False], "B": [1, 0]})
>>> df
       A  B
0   True  1
1  False  0

>>> df.any(axis="columns")
0    True
1    False
dtype: bool

Aggregating over the entire DataFrame with ``axis=None``.

>>> df.any(axis=None)
True

`any` for an empty DataFrame is an empty Series.

>>> pd.DataFrame([]).any()
Series([], dtype: bool)
File:      ~/python_projects/.venv/lib/python3.13/site-packages/pandas/core/series.py
Type:      function
In [ ]: