0% found this document useful (0 votes)
23 views

DMT Function

The document contains code snippets demonstrating various pandas functions and operations for working with DataFrames and Series. These include functions for aggregation, filtering, transforming, handling missing values, slicing rows and columns, descriptive statistics, and more. NumPy functions are also used for array operations like cumsum and cumprod.

Uploaded by

Muhammad Salman
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

DMT Function

The document contains code snippets demonstrating various pandas functions and operations for working with DataFrames and Series. These include functions for aggregation, filtering, transforming, handling missing values, slicing rows and columns, descriptive statistics, and more. NumPy functions are also used for array operations like cumsum and cumprod.

Uploaded by

Muhammad Salman
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

import numpy as np

arr = [1, 2, 3, 4]

print(np.cumsum(arr))

# Output: [1 3 6 10]

import numpy as np

arr = [1, 2, 3, 4]

print(np.cumprod(arr))

# Output: [1 2 6 24]

import pandas as pd

df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})

print(df.memory_usage())

# Output: Index 80

# A 24

# B 24

# dtype: int64

s = {1, 2, 3}

fset = frozenset(s)

print(fset) # Output: frozenset({1, 2, 3})

# Attempting to add an element to the frozenset will result in an error

fset.add(4) # Output : 'frozenset' object has no attribute 'add'


arr = [1, 2, 3, 4]

arr.reverse()

print(arr) # Output: [4, 3, 2, 1]

import pandas as pd

df = pd.DataFrame({'A': [1, 2, np.nan, 4], 'B': [5, np.nan, 7, 8]})

df.fillna(0)

# Output:

# A B

# 0 1.0 5.0

# 1 2.0 0.0

# 2 0.0 7.0

# 3 4.0 8.0

import pandas as pd

df = pd.DataFrame({'A': [1, 2, np.nan, 4], 'B': [5, np.nan, 7, 8]})

print(df.isna())

# Output:

# A B

# 0 False False

# 1 False True

# 2 True False

# 3 False False
import pandas as pd

df = pd.DataFrame({'A': [1, 2, np.nan, 4], 'B': [5, np.nan, 7, 8]})

print(df.isnull().sum())

# Output:

#A 1

#B 1

# dtype: int64

import pandas as pd

df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})

print(df.head(2))

# Output:

# A B

#0 1 4

#1 2 5

import pandas as pd

df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})

print(df.tail(2))

# Output:

# A B

#1 2 5

#2 3 6

import pandas as pd

df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})

df.info()

# Output:

# <class 'pandas.core.frame.DataFrame'>
# RangeIndex: 3 entries, 0 to 2

# Data columns (total 2 columns):

# A 3 non-null int64

# B 3 non-null int64

# dtypes: int64

import pandas as pd

df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})

print(df.sample(2))

# Output:

# A B

#2 3 6

#0 1 4

import pandas as pd

df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})

df.info()

# Output:

# <class 'pandas.core.frame.DataFrame'>

# RangeIndex: 3 entries, 0 to 2

# Data columns (total 2 columns):

# A 3 non-null int64

# B 3 non-null int64

# dtypes: int64(2)

# memory usage: 176.0 bytes


import pandas as pd

df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})

print(df.sample(2))

# Output:

# A B

#2 3 6

#0 1 4

import pandas as pd

df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})

print(df.shape)

# Output: (3, 2)

import pandas as pd

df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})

print(df.nsmallest(2, 'A'))

# Output:

# A B

#0 1 4

#1 2 5

import pandas as pd

df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})

print(df.nlargest(2, 'A'))

# Output:

# A B

#2 3 6

#1 2 5
import pandas as pd

df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})

df.eval("C = A + B", inplace=True)

print(df)

# Output:

# A B C

#0 1 4 5

#1 2 5 7

#2 3 6 9

import pandas as pd

df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})

print(df.loc[1])

# Output:

#A 2

#B 5

# Name: 1, dtype: int64

import pandas as pd

df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})

print(df.iloc[1])

# Output:

#A 2

#B 5

# Name: 1, dtype: int64

import pandas as pd

df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})

print(df.clip(1, 4))
# Output:

# A B

#0 1 4

#1 2 4

#2 3 4

import pandas as pd

df1 = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})

df2 = pd.DataFrame({'C': [7, 8, 9], 'D': [10, 11, 12]})

result = pd.concat([df1, df2],…

print(chr(65))

# Output: 'A'

import pandas as pd

df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})

df = df.rename(columns={'A': 'X', 'B': 'Y'})

print(df)

# Output:

# X

import pandas as pd

df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})

print(df.query('A > 1'))

# Output:

# A B

#2 3 6
import pandas as pd

df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})

print(df.describe())

# Output:

# A B

# count 3.000000 3.000000

# mean 2.000000 5.000000

# std 1.000000 1.000000

# min 1.000000 4.000000

# 25% 1.500000 4.500000

# 50% 2.000000 5.000000

# 75% 2.500000 5.500000

# max 3.000000 6.000000

import pandas as pd

df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})

df['A'] = df['A'].astype(float)

print(df.dtypes)

# Output:

# A float64

#B int64

# dtype: object

import pandas as pd

df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})

print(df.columns)

# Output: Index(['A', 'B'], dtype='object')


import pandas as pd

df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})

df = df.drop(columns=['A'])

print(df)

# Output:

# B

#0 4

#1 5

#2 6

import pandas as pd

df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})

print(df.pct_change())

# Output:

# A B

#0 NaN NaN

# 1 1.000000 0.250000

# 2 0.500000 0.200000

import pandas as pd

df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})

print(df.rank())

# Output:

# A B

# 0 1.0 1.0

# 1 2.0 2.0

# 2 3.0 3.0
string = "Hello World!"

print(string.split())

# Output: ['Hello', 'World!']

string = "Hello World! "

print(string.rstrip())

# Output: 'Hello World!'

string = " Hello World!"

print(string.lstrip())

# Output: 'Hello World!'

import pandas as pd

df = pd.DataFrame({'A': [[1, 2], [3, 4]]})

print(df.explode('A'))

# Output:

# A

#0 1

#0 2

#1 3

#1 4

import pandas as pd

df = pd.DataFrame({'A': [1, 2, 3, 2, 1], 'B': [4, 5, 6, 5, 4]})

print(df.A.unique())

# Output: [1 2 3]

You might also like