assistant: Basic Operations:
python
Copy code
import pandas as pd

# Creating a DataFrame
df = pd.DataFrame({
    'col1': [1, 2, 3, 4],
    'col2': [10, 20, 30, 40],
    'col3': ['a', 'b', 'c', 'd']
})

# Head and Tail
print(df.head(2)) # First 2 rows
#    col1  col2 col3
# 0     1    10    a
# 1     2    20    b

print(df.tail(2)) # Last 2 rows
#    col1  col2 col3
# 2     3    30    c
# 3     4    40    d

# Shape
print(df.shape) # (4, 3)

# Columns
print(df.columns) # Index(['col1', 'col2', 'col3'], dtype='object')

# Info
print(df.info()) 
# <class 'pandas.core.frame.DataFrame'>
# RangeIndex: 4 entries, 0 to 3
# Data columns (total 3 columns):
#  col1    4 non-null int64
#  col2    4 non-null int64
#  col3    4 non-null object
# dtypes: int64(2), object(1)
# memory usage: 208.0+ bytes
# None

# Describe
print(df.describe())
#             col1        col2
# count  4.000000   4.000000
# mean   2.500000  25.000000
# std    1.290994  14.142136
# min    1.000000  10.000000
# 25%    1.750000  17.500000
# 50%    2.500000  25.000000
# 75%    3.250000  32.500000
# max    4.000000  40.000000

# Selecting Columns
print(df['col1'])
# 0    1
# 1    2
# 2    3
# 3    4
# Name: col1, dtype: int64

# Slicing Rows
print(df[1:3])
#    col1  col2 col3
# 1     2    20    b
# 2     3    30    c

# Boolean Indexing
print(df[df['col1'] > 2])
#    col1  col2 col3
# 2     3    30    c
# 3     4    40    d
Groupby Operations:
python
Copy code
import pandas as pd

# Creating a DataFrame
df = pd.DataFrame({
    'col1': [1, 2, 3, 4, 5],
    'col2': [10, 20, 30, 40, 50],
    'col3': ['a', 'b', 'c', 'd', 'e']
})

# Groupby
grouped = df.groupby('col3')

# Mean
print(grouped.mean())
#      col1  col2
# col3            
# a      1    10
# b      2    20
# c      3    30

Vectorized Operation:

python
Copy code
import pandas as pd
import numpy as np

# Creating a DataFrame
df = pd.DataFrame({
    'col1': [1, 2, 3, 4],
    'col2': [10, 20, 30, 40],
    'col3': ['a', 'b', 'c', 'd']
})

# Arithmetic Operations
print(df['col1'] + df['col2'])
# 0    11
# 1    22
# 2    33
# 3    44

# Comparison Operations
print(df['col1'] > 2)
# 0    False
# 1    False
# 2     True
# 3     True

# Reshaping
print(df.pivot(index='col1', columns='col3', values='col2'))
# col3    a    b    c    d
# col1                    
# 1    10.0  NaN  NaN  NaN
# 2     NaN  20.0  NaN  NaN
# 3     NaN  NaN  30.0  NaN
# 4     NaN  NaN  NaN  40.0

# Join
df1 = pd.DataFrame({
    'col1': [1, 2, 3],
    'col2': [10, 20, 30]
})
df2 = pd.DataFrame({
    'col1': [2, 3, 4],
    'col2': [20, 30, 40]
})
print(pd.merge(df1, df2, on='col1'))
#    col1  col2_x  col2_y
# 0     2     10     20
# 1     3     30     30
Cython Routine:

python
Copy code
# Cythonize your code
%load_ext Cython

def sum_of_lists(N):
    total = 0
    for i in range(N):
        total += i
    return total

# Compiling the code to C
%cython -a sum_of_lists.py
List Comprehension:

python
Copy code
# Creating a DataFrame
df = pd.DataFrame({
    'col1': [1, 2, 3, 4],
    'col2': [10, 20, 30, 40],
    'col3': ['a', 'b', 'c', 'd']
})

# List Comprehension
col1_squared = [i**2 for i in df['col1']]
print(col1_squared)
# [1, 4, 9, 16]
DataFrame.apply():

python
Copy code
import pandas as pd

# Creating a DataFrame
df = pd.DataFrame({
    'col1': [1, 2, 3, 4],
    'col2': [10, 20, 30, 40],
    'col3': ['a', 'b', 'c', 'd']
})

# Define a function
def add_10(x):
    return x + 10

# Apply the function
print(df['col2'].apply(add_10))
# 0    20
# 1    30
# 2    40
# 3    50


"Vectorized Operations:
python
Copy code
import pandas as pd
import numpy as np

Creating a DataFrame
df = pd.DataFrame({
'col1': [1, 2, 3, 4],
'col2': [10, 20, 30, 40],
'col3': [1, 2, 3, 4]
})

Arithmetic Operations
print(df['col1'] + df['col2'])

0 11
1 22
2 33
3 44
dtype: int64
print(df['col1'] - df['col2'])

0 -9
1 -18
2 -27
3 -36
dtype: int64
print(df['col1'] * df['col2'])

0 10
1 40
2 90
3 160
dtype: int64
print(df['col1'] / df['col2'])

0 0.100000
1 0.100000
2 0.100000
3 0.100000
dtype: float64
Comparison Operations
print(df['col1'] > 2)

0 False
1 False
2 True
3 True
Name: col1, dtype: bool
print(df['col1'] < 2)

0 True
1 False
2 False
3 False
Name: col1, dtype: bool
print(df['col1'] == 2)

0 False
1 True
2 False
3 False
Name: col1, dtype: bool
Reshaping
print(df.T)

0 1 2 3
col1 1 2 3 4
col2 10 20 30 40
col3 1 2 3 4
Joins
df2 = pd.DataFrame({
'col1': [1, 2, 3, 4],
'col2': [100, 200, 300, 400]
})

print(pd.concat([df, df2], axis=1))

col1 col2 col3 col1 col2
0 1 10 1 1 100
1 2 20 2 2 200
2 3 30 3 3 300
3 4 40 4 4 400
Reduction Operations
print(df.sum())

col1 10
col2 100
col3 10
dtype: int64
print(df.min())

col1 1
col2 10
col3 1
dtype: int64
print(df.max())

col1 4
col2 40
col3 4
dtype: int64
print(df.mean())

col1 2.5
col2 25.0
col3 2.5
dtype: float64
"