beginof
November 15, 2022, 2:34am
1
How to use the if elif else
condition to insert the data into the existing column?
Example, if the columnA = 'abc'
then columnB
return '123'
, if the columnA = 'efg'
then columnB
return '345'
, else
return '0'
.
so based on the data it should be:
if the Sale id = 'sale001'
then Sale no
return 'clo sale'
, elif the Sale id = 'sale002'
then Sale no
return 'blo sale'
, else return ''
.
Hello @beginof ,
I think you’ve got the gist of it.
Instead of thinking about it being a table, think of it as a dataframe that you are manipulating and then return the resulting dataframe to a table.
1 Like
beginof
November 15, 2022, 3:13am
3
HI @jinnyzor ,
Yes, initially I also think about this concept. But how if the value that i need to put as a link or button which able to connect to another page and so on?
To allow for inserting links into a datatable, you can use a markdown column.
You can do this by passing presentation: markdown
for those specific columns. And then in your data, you can create the links via markdown:
beginof
November 15, 2022, 6:52am
5
HI @jinnyzor ,
Can you share some sample code?
Actually, I had a dash_table.DataTable
require to insert the additional value which is a link or button that can connect to another page of the multi-page dashboard.
Can you give me the structure of the target link?
beginof
November 15, 2022, 7:06am
7
http://127.0.0.1:8050/page2
some kind like this.
beginof
November 24, 2022, 9:02am
8
Hi,
When I add the def info2(row)
into the def update_table(selection)
in order to insert the data for the ‘Sale no’ column, it will show error:
Invalid argument data[7].refer
passed into DataTable with ID “tabletrn”.
Expected one of type [string, number, boolean].
dbc.Row([
dbc.Col([
html.P("Transaction status detail:",
style={"textDecoration":"underline"}),
dash_table.DataTable(id='tabledetail',
columns=[
{'name': 'Product', "id": 'product'},
{'name': 'Sale id', "id": 'info'},
{'name': 'Amount', "id": 'amount'},
{'name': 'Sale no', "id": 'refer'},
],
editable=True,
style_cell={'textAlign': 'left'},
),
]),
]),
@app.callback(
Output('tabletrn', 'data'),
Input('trnid_dd', 'value')
)
def update_table(selection):
if len (selection) == 0:
return dash.no_update
else:
dff = df[df['Sale id'] == selection]
transpose = dff.melt(id_vars = ['Product' ,'Amount']
,var_name = ['header']
,value_name = 'info')
def info2(row):
if row['header'] == 'sale001':
return 'clo sale'
elif row['header'] == 'sale002':
return 'blo sale'
# else:
return ''
info['refer'] = info.apply (lambda row: info2(row), axis=1)
info3 = info.copy()
columns = info3[['header','info', 'refer']]
data= columns.to_dict('records')
return data
Any idea?
What columns does this other table have? My guess is that it doesn’t have this column of refer.
beginof
November 24, 2022, 9:44am
10
sorry, i put in wrong information.
already amended.
Before the return, print the columns.
What does it show?
beginof
November 25, 2022, 1:38am
12
Unable to print in the table callback
but I had try to print it without the callback, the result is exactly what I expect,
there is not issue if print in dataframe
therefore, I only move the solution into the table callback
but when move in, the error came out in the table callback
Can you show snippet of what the df looks like before you try to return it?
beginof
November 25, 2022, 3:12am
14
like the first image i show
I saw there is no issue in dataframe when I print it to the .csv, then I only apply the same method to my data table.
But error came out.
Without the second def, there is no issue for the data table.
The second def I m using to add in the additional information for specified row data into the new column.
Instead of doing that function, just make it a straight if then statement.
if row['header'] == 'sale001':
info[‘refer’] = 'clo sale'
elif row['header'] == 'sale002':
info[‘refer’] = 'blo sale'
else:
info[‘refer’] = ‘’
beginof
November 25, 2022, 7:27am
16
I had tried this solution last time, but it will show error
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().