Probleme Reading csv column , Python dont recognize column even its right spelling

Hello Jupyeter community

I have a probleme with reading column , When i want to make calculation , even all things is right, the speling name in script is the same as in my csv file, VS code dont read the coulmn name even its speling 100% correct and send all times Error, please help me

  1. Élément

def classify_return(Investment_Return):
if Investment_Return> good_return_threshold:
return’Good’
else:
return’Not good’

This code is correct:

  1. Élément

Then i write :

mutual_funds[‘Return_Classification’]= mutual_funds[‘Investment_Return’].apply(classify_return)

Error is this : KeyError: ‘Investment_Return’

The above exception was the direct cause of the following exception:

Please, consider if this is pertinent to this forum about Jupyter? If you ran this Python code in a more traditional way, such as in the console or as the content a script you called from the command line for Python run, would it give the same outcome? (If you are new to running Python code, you may not be able to do this exercise in your mind, and should actually step through the exercise.) If so, it isn’t a Jupyter issue and so not pertinent to this forum. You’ll want to seek help in a pertinent location.


Your provided code is unuseable and nearly unreadable.

For pasting code in so it is readable in forums such as this & useable by those you want to help you, you’ll want to learn about ‘block code formatting’ here. (Or see about ‘fenced code blocks’ here. They are both the same thing if you look into the details. They just have slightly different terms used in the two locations.

2 Likes

Sr , Im not beguinner , i run c,c++ since 2018 and all what i want is to get answer not somethings innapropriate like your message , Sr i want answer about coulmn not reading in script or maybe you are not able to help me and in this case let another one get help to me

Please read Getting good answers to your questions. I would suggest paying particular attention to the section ‘Help others reproduce the problem’, in the link to How do I ask a good question? at the bottom there.


Your code and issue is not reproducible as provided.
Here is a minimal, toy example adapted from the incomplete code you provided:

import pandas as pd
mutual_funds = pd.DataFrame({'A': range(1, 6),
                   'B': range(10, 0, -2),
                   'Investment_Return': range(100, 0, -20),
                   'C C': range(10, 5, -1)})

good_return_threshold = 51
def classify_return(Investment_Return):
    if Investment_Return> good_return_threshold:
        return 'Good'
    else:
        return 'Not good'
mutual_funds['Return_Classification']= mutual_funds['Investment_Return'].apply(classify_return)

(Notice the code is formatted to be pasteable.).
This code works fine, as can be observed by clicking here and running it when the session starts. (Or click here if you want it in JupyterLite, which is a nice option when MyBinder-served sessions are being slow to come up.) (Here is the boring static view, but the two links prior are much more useful and I suggest using those primarily.)

In other words, I don’t see your reported issue with the limited code and information provided.
If however, I change the equivalent line you had an issue with to mutual_funds['Return_Classification']= mutual_funds['Z'].apply(classify_return), then I get KeyError: 'Z' because there is no column named Z in my dataframe.

1 Like

Hello Sr thanks you for your Reply ,

its sems your know well code. And instructed person,

Otherwise . You will help me more if you tell me Whats IS the issue with m’y script, file that i load. Because the name of the column is mentioned correcltly in the script.

Error: python dont detect coulmn with name “Investment_Return”

If you read through everything already written above and follow it, you’d see you’d need to provide more details for anyone to be able to help you.

Did you run mutual_funds.columns before you ran your line mutual_funds[‘Return_Classification’]= mutual_funds[‘Investment_Return’].apply(classify_return)? What was the result?
Is there a column named Investment_Return?

When I run mutual_funds.columns at the equivalent point in my demonstration notebook, I see the following

Index(['A', 'B', 'Investment_Return', 'C C'], dtype='object')`

And thus I am sure there is a column named that before I run, mutual_funds['Return_Classification']= mutual_funds['Investment_Return'].apply(classify_return).

1 Like

JupyterLab This is my code

  1. Élément

import numpy as np

import pandas as pd

  1. Élément
    #step 1 load data from excel file
    mutual_funds=pd.read_csv(‘mutual_funds.csv’) :white_check_mark:

  2. Élément
    mutual_funds.head()
    ||Fund Name;Investment_Amount;Current_Value;Investment_Return;Return_Classification;Date_Invested|
    |—|—|
    |0|Fund A;1000;1200;0.2;Good;01/01/2023|
    |1|Fund B;1500;1300;-0.1333;Not good;01/02/2023|
    |2|Fund C;2000;2500;0.25;Good;01/03/2023|
    |3|| :white_check_mark:

  3. Élément
    #calassify investement return
    def classify_return(Investment_Return):
    if Investment_Return> good_return_threshold:
    return’Good’
    else:
    return’Not good’ :white_check_mark:

  4. Élément
    #Apply the classification to the dataframe

mutual_funds[‘Return_Classification’]= mutual_funds[‘Investment_Return’].apply(classify_return)
:persevere: :x:
Error : ‘Investment_Return’ Not exist

This is still not complete, addressing my last questions, or formatted than anyone can use it. See my examples above and follow them using advice about ‘fenced code blocks’.

Why do I say this is not complete:
Where is good_return_threshold set?

Why do I say it doesn’t address what I asked:
You still didn’t run mutual_funds.columns before you ran your code that isn’t working and provide the result.
I’d also suggest you run len(mutual_funds.columns) and compare that result to what you expect.
If you parsed your CSV file correctly, how many columns do you expect?

It seems to me you either didn’t share the result of mutual_funds.head() well in text or you didn’t parse the CSV correctly. If the latter option is the case then it means there shouldn’t be a green check mark for step 3. That being the case seems likely, since you have after running mutual_funds.head() the following:

|—|—|

Meaning there may only be the Row Indexes and a single column. Hence, there is no column Investment_Return, like the error keeps telling you.
But of course, maybe you just didn’t share the result of mutual_funds.head() well in text?

2 Likes