Add A New Column

Sat 17 May 2025

title: "Add a New Column" author: "Rj" date: 2019-04-20 description: "List Test" type: technical_note draft: false


import pandas as pd
data = {
    'city' : ['Toronto', 'Montreal', 'Waterloo'],
    'points' : [80, 70, 90]
}
data
{'city': ['Toronto', 'Montreal', 'Waterloo'], 'points': [80, 70, 90]}
type(data)
dict
df = pd.DataFrame(data)
df
city points
0 Toronto 80
1 Montreal 70
2 Waterloo 90
df = df.assign(code = [1, 2, 3])
df
city points code
0 Toronto 80 1
1 Montreal 70 2
2 Waterloo 90 3


Score: 10

Category: data-wrangling


Advanced-Custom-Lambda

Sat 17 May 2025

title: "Advanced Custom Lambda" author: "Rj" date: 2019-04-22 description: "-" type: technical_note draft: false


import numpy as np
import pandas as pd
# Check the student passed either math or passed at least in 2 subjects. If no condidition matched, consider them as failed
def pass_math_or_two_subjects(row):
    if(row.maths > 34):
        return …

Category: data-wrangling

Read More

Apply Custom Function

Sat 17 May 2025

title: "Apply Custom Function" author: "Rj" date: 2019-04-20 description: "List Test" type: technical_note draft: false


import pandas as pd
data = {
    'city' : ['Toronto', 'Montreal', 'Waterloo'],
    'points' : [80, 70, 90]
}
data
{'city': ['Toronto', 'Montreal', 'Waterloo'], 'points': [80, 70, 90]}
type(data)
dict
df = pd.DataFrame(data)
df

Category: data-wrangling

Read More

Apply Function-2933

Sat 17 May 2025

title: "Apply Function" author: "Rj" date: 2019-04-20 description: "List Test" type: technical_note draft: false


import pandas as pd
data = {
    'city' : ['Toronto', 'Montreal', 'Waterloo', 'Toronto', 'Waterloo', 'Toronto', 'Toronto'],
    'points' : [80, 70, 90, 85, 79, 82, 200]
}
data
{'city': ['Toronto',
  'Montreal',
  'Waterloo',
  'Toronto',
  'Waterloo',
  'Toronto',
  'Toronto'],
 'points': [80, 70, 90, 85, 79 …

Category: data-wrangling

Read More

Astype 2

Sat 17 May 2025

title: "Astype 2" author: "Rj" date: 2019-04-24 description: "-" type: technical_note draft: false


import numpy as np
import pandas as pd
marks = [
    [90, 87],
    [90, 95],
    [92, 95]
]
marks
[[90, 87], [90, 95], [92, 95]]
df = pd.DataFrame(marks, columns=['maths', 'science'])
df

Category: data-wrangling

Read More

Categorical To Numerical

Sat 17 May 2025

title: "Categorial to Numerical" author: "Rj" date: 2019-04-24 description: "-" type: technical_note draft: false


import numpy as np
import pandas as pd
data = {
    'sam' : ['archery', 'badminton', 'athletics', 'cycling_road', 'canoe_sprint', 'boxing'],
    'medal' : ['gold', 'silver', 'bronze', 'gold', 'gold', 'silver']
}
df = pd.DataFrame(data)
df

Category: data-wrangling

Read More

Change-Columns-Based-On-Other-Column

Sat 17 May 2025

title: "Change Column Based on Other Column" author: "Rj" date: 2019-04-24 description: "-" type: technical_note draft: false


import numpy as np
import pandas as pd
df = pd.read_csv('marks.csv')
df
student language …

Category: data-wrangling

Read More

Check Datatypes

Sat 17 May 2025

title: "Check Data Types" author: "Rj" date: 2019-04-24 description: "-" type: technical_note draft: false


import numpy as np
import pandas as pd
df = pd.read_csv("/Users/rajacsp/datasets/sales_data_types.csv")
df
Customer Number …

Category: data-wrangling

Read More

Csv-To-Dataframe

Sat 17 May 2025

title: "CSV to Dataframe" author: "Rj" date: 2019-04-22 description: "-" type: technical_note draft: false


import numpy as np
import pandas as pd
csv = pd.DataFrame.from_csv('uk-500.csv')
/Users/rajacsp/anaconda3/envs/py36/lib/python3.6/site-packages/ipykernel_launcher.py:1: FutureWarning: from_csv is deprecated. Please use read_csv(...) instead. Note that …

Category: data-wrangling

Read More

Custom-Function-As-Lambda

Sat 17 May 2025

title: "Custom Function as Lambda" author: "Rj" date: 2019-04-22 description: "-" type: technical_note draft: false


import numpy as np
import pandas as pd
def apply_math_special(row):
    return (row.maths *2 + (row.language/2) + (row.history/3) + (row.science/4))
df = pd.read_csv('abc.csv')
df

Category: data-wrangling

Read More
Page 1 of 6

Next »