0

I have a def where it is pulling data from a CSV file and only showing a certain row & column. The issue I am having is when printing it adds a "none" row at the end.


def last_3rd_price():
    
    df1 = pd.read_csv('test.csv')
    row_start = df1.index[-3]
    row_end = df1.index[-3]
    last_3rd_price = df1.loc[row_start:row_end, 'Price']
            
    for price in last_3rd_price:
        print(price)
             
print(last_3rd_price())

Data:

7129.32 Dec-15-2019 12:32:37
7129.32 Dec-15-2019 12:32:43
7129.32 Dec-15-2019 12:33:11
7129.26 Dec-15-2019 12:34:35
7129.26 Dec-15-2019 12:34:40
7129.26 Dec-15-2019 12:35:15
7129.26 Dec-15-2019 12:35:33
7127.56 Dec-15-2019 12:42:51
7128.43 Dec-15-2019 12:44:39

Here is the output:

7129.26
None

I would like to know how to get rid of the last none line. I have tried to use drop() & dropna() with no success

3
  • can you just slice it with [:-1] Commented Dec 15, 2019 at 18:52
  • Show us your data, and read the Pandas docs. Also, variable and function names should follow the lower_case_with_underscores style. Commented Dec 15, 2019 at 18:56
  • I have added the data in the original post Commented Dec 15, 2019 at 19:22

2 Answers 2

1

You’re printing the result of the function, which is None, as it returns nothing.

You should have the function return a value, and fix those names.

Sign up to request clarification or add additional context in comments.

1 Comment

Such an easy solution, I was banging my head about this. Thank you so much
0

Is it "None" (string) or None you have in there? I believe None will be replaced by np.NaN. I think you have is "None" (string). Try this:

df = df.replace(to_replace='None', value=np.nan).dropna()

1 Comment

I tried this however it still returns the last line as None

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.