ShaneB
ShaneB

Reputation: 3

Python Pandas drop the last row showing none

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

Upvotes: 0

Views: 462

Answers (2)

AMC
AMC

Reputation: 2702

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.

Upvotes: 1

davidbilla
davidbilla

Reputation: 2222

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()

Upvotes: 0

Related Questions