boss
I am experiencing an error with my code when I attempt to assign the value of df[column] to a variable and subsequently check it using an if condition. I would appreciate it if you could review my code and provide the correct version. Thank you in advance for your assistance.
this is my code
import pandas as pd
import streamlit as st
if ‘df_base’ not in st.session_state:
st.session_state.df_base = pd.DataFrame({‘a’: [1, 2, 3],‘aa’: [4, 5, 6], ‘b’: [11, 22, 33], ‘c’: [‘x’, ‘y’, ‘z’]})
df = st.session_state.df_base
st.markdown(‘Column “b” is editable, and will affect column “d” = “a” + “b”’)
c1, c2, c3, c4 = st.columns(4, gap=“small”)
with c4:
df_right = df[[‘aa’]]
val1 = df[‘a’]
if val1>1:
df_right[‘aa’] = df[‘a’] + df[‘a’]
st.dataframe(df_right)
st.markdown(‘TODO:\n’
‘* hide the indices of the edit & right dfs\n’
‘* “squeeze” the dfs together’)
I am new to Streamlit and would appreciate guidance on how to retrieve column values for conditional checks, which will be used for further processing of another DataFrame column.
It is not a streamlit issue, it is a pandas syntax. df['a'] is a column, when you compare it to a scalar like df['a'] > 1 it returns a boolean column with the results of each element in that column with this scalar.
so for your example, if df['a'] = [1,2,3], the results of df['a'] > 1 will be [False, True, True].
So your if-statement is unclear on what you want to do with those results.
you can use the any or all functions to convert the results into a single boolean value according to your desired logic.
thank you for your reply
I would like to verify the values in the CSV file’s column against the DataFrame. If the quantity exceeds 100, please apply a 10% discount in the subsequent column; otherwise, indicate 0 in the next column.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.