Search
Tricks
import pandas as pd

Explode

also new for pandas 0.25

List

If have a list within a column and you want to create a row for each item in that list.

df = pd.DataFrame({
    'article': ['a', 'b', 'c'],
    'tags': [['happy', 'fun'], ['sad', 'frustrating', 'stressful'], ['ಠ_ಠ']],
    'views': [100, 10, 10000]
})

df
article tags views
0 a [happy, fun] 100
1 b [sad, frustrating, stressful] 10
2 c [ಠ_ಠ] 10000
df.explode('tags')
article tags views
0 a happy 100
0 a fun 100
1 b sad 10
1 b frustrating 10
1 b stressful 10
2 c ಠ_ಠ 10000

Comma-seperated

If have comma seperated values within a column and you want to create a row for each item in the string

df = pd.DataFrame({
    'article': ['a', 'b', 'c'],
    'tags': ['happy,fun', 'sad,frustrating,stressful', 'ಠ_ಠ'],
    'views': [100, 10, 10000]
})

df
article tags views
0 a happy,fun 100
1 b sad,frustrating,stressful 10
2 c ಠ_ಠ 10000
(
    df
    .assign(tags=df.tags.str.split(","))
    .explode('tags')
)
article tags views
0 a happy 100
0 a fun 100
1 b sad 10
1 b frustrating 10
1 b stressful 10
2 c ಠ_ಠ 10000

Query

for easy to read sql-like slices, good for chaining

(
    df
    .query('views > 10 & views < 10000')
)
article tags views
0 a happy,fun 100

can also pass variables with @

threshold = 100
(
    df
    .query('views == @threshold')
)
article tags views
0 a happy,fun 100

Multiple Conditions

Sometimes we want to filter our dataframe using a list

some_list = ['a', 'b']
df[df.article.isin(some_list)]
article tags views
0 a happy,fun 100
1 b sad,frustrating,stressful 10

we can reverse this easily with ~

df[~df.article.isin(some_list)]
article tags views
2 c ಠ_ಠ 10000

Assign

Skipping basics since you can just look here: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.assign.html

Create columns with variable names, also f strings (prob cleaner to use than {}.format()

col_name = 'cat'
(
    df
    .assign(
        **{f'some_{col_name}': ['haku', 'nagi', 'poki']}
    )
    .style.set_caption("some random dataframe I imagined")
)
some random dataframe I imagined
article tags views some_cat
0 a happy,fun 100 haku
1 b sad,frustrating,stressful 10 nagi
2 c ಠ_ಠ 10000 poki

can also set a caption for the dataframe lol