File size: 995 Bytes
674f526
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
from datetime import datetime

import pandas as pd


def get_table_download_link(df: pd.DataFrame) -> str:
    """Generates a link for download the `df` locally as a csv file.

    Args:
        df: the dataframe to download

    Returns:
        href link
    """
    import base64

    csv = df.to_csv(index=False)
    b64 = base64.b64encode(csv.encode()).decode()
    return f'<a href="data:file/csv;base64,{b64}" download="data_{datetime.now()}.csv">download</a>'


def filter_df(df: pd.DataFrame, filter_: str) -> pd.DataFrame:
    """Takes a dataframe and a `filter_` keyword, returns all the rows that contain the value `filter_` in any column

    Args:
        df: pandas dataframe
        filter_: the string to search in the dataframe

    Returns:
        filtered dataframe
    """
    import numpy as np

    mask = np.column_stack(
        [df[col].astype(str).str.contains(filter_, na=False) for col in df]
    )
    filtered_df = df.loc[mask.any(axis=1)]
    return filtered_df