I'm trying to make a conditional column using pandas.DataFrame.apply.
The goal is : when I click on the button Click here
, I should get a csv with only the row matching the community chosen (in our case, a dataframe with one single row and one single column=Community).
Code:
import pandas as pd
from IPython.display import HTML
import base64
df = pd.DataFrame({'Community': ['StackOverflow','GISExchange', 'DBAExchange']})
df.insert(1, "URL", 'Click here')
csv = df.to_csv(index=False)
b64 = base64.b64encode(csv.encode())
payload = b64.decode()
df['URL'] = df['URL'].apply(
lambda x: f'<a download="{x}.csv" href="data:text/csv;base64,{payload}" target="_blank">{x}</a>')
HTML(df.to_html(escape=False))
The problem with the code above is that the hyperlink Click here
, gives a csv with the hole dataframe (including the three rows).
Do you know how to fix that ? Do you have any suggestions, please ?
After some struggling, I end up with the code below that answer my question.
I first created a function that decode the csv and so I can filter the dataframe.
Then, replaced the .apply method with a for loop.
import pandas as pd
from IPython.display import HTML
import base64
df = pd.DataFrame({'Community': ['StackOverflow','GISExchange', 'DBAExchange']})
df.insert(1, "URL", 'Click here')
def pyld(val):
csv = df[df['Community']==val]['Community'].to_csv(index=False)
b64 = base64.b64encode(csv.encode())
payload = b64.decode()
return payload
for i, row in df.iterrows():
row['URL'] = f'<a download="data.csv" href="data:text/csv;base64,{pyld(df["Community"][i])}" target="_blank">Click here</a>'
HTML(df.to_html(escape=False))