QBoard » Artificial Intelligence & ML » AI and ML - Python » Pandas NameError: name 'df' is not defined

Pandas NameError: name 'df' is not defined

  • This is my problem: Cousera course on Apllied Data Science in Python I am doing Assigment 2.

    Question 1 Which country has won the most gold medals in summer games? This function should return a single string value.

    This my code:

    def answer_one():
      return df[df['Gold'] == df['Gold'].max()].index(0)
    answer_one()

     

    This is the error which I am getting:

    NameError: name 'df' is not defined
    
      October 15, 2021 2:10 PM IST
    0
  • def func_nb():
       data = pd.read_csv('dummycsv.csv')
       df = DataFrame(data)
       df['Tag'] = df['Tag'].map(lambda x: re.sub(r'\W+', '_', x))
       return df
    
    df = func_nb()

     

    After that you can use your dataframe, because it now exists outside of the your function.

     
      October 16, 2021 12:50 PM IST
    0
  • import streamlit as st
    import pandas as pd
    from pandas import DataFrame
    import matplotlib.pyplot as plt
    import plotly.express as px
    
    #configuration
    st.set_option('deprecation.showfileUploaderEncoding', False)
    st.title("Data Visualisation App")
    
    # Add sidebar
    st.sidebar.subheader("Visualisation settings")
    
    # Setup file upload
    global df
    uploaded_file = st.sidebar.file_uploader(label="Upload your Excel file", type = ['csv','xlsx'])
    if uploaded_file is not None:
        df = pd.read_csv(uploaded_file)
        st.write("Data Set")
        st.dataframe(df,3000,500)
    
    if st.checkbox('Show raw data'):
        st.subheader('Raw data')
        st.write(df)
      November 22, 2021 12:10 PM IST
    0
  • You will have to let func_nb() return the df and make sure to call the function func_nb()
      November 22, 2021 6:05 PM IST
    0
  • Please try to use .Hope so it will work

    return df['Gold'].idxmax()
    
    
      October 28, 2021 4:41 PM IST
    0