I load an Excel file into a pandas dataframe, and it displays correctly. However, when I get the first column for the graph’s x value, I get the error Key Error.“[‘student’] not in index” I tried a number of methods, but they didn’t work.
Hey there, thanks for sharing your question and the error details! It seems likely that your KeyError is because the column ‘student’ does not exist in your DataFrame ddg2 after the groupby/reset_index operation. Instead, your columns are probably named ‘STDID’, ‘STD_NAME’, and the result of .count() on your grouped columns (like ‘C-1’, ‘C-2’, ‘C-3’), but not ‘student’.
To fix this, you should use the actual column names present in ddg2. You can check the column names with st.write(ddg2.columns) before selecting columns. For example, if you want to select ‘STD_NAME’, ‘C-1’, ‘C-2’, ‘C-3’, your code should look like:
mfa = ddg2[['STD_NAME', 'C-1', 'C-2', 'C-3']]
If you want to rename ‘STD_NAME’ to ‘student’, you can do so with ddg2.rename(columns={‘STD_NAME’: ‘student’}, inplace=True) before selecting columns. My understanding is that this approach should resolve your KeyError, as discussed in similar community questions about column name mismatches and KeyErrors in pandas DataFrames.