Home > Net >  How can I get the last item from a row in an array?
How can I get the last item from a row in an array?

Time:08-26

I have a CSV file that looks like this:

5/15/1987   18.58

6/15/1987   18.86

7/15/1987   19.86

8/15/1987   18.98

9/15/1987   18.31

10/15/1987  18.76

11/15/1987  17.78

12/15/1987  17.05

1/15/1988   16.75

...

I am using this code to read and parse the file:

import textract
import numpy
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import csv

oil_value = []

with open(r"C:\Users\derek\Downloads\brent-monthly.csv", "r") as file:
    oil = csv.reader(file, delimiter=',', quotechar='|')

    for lines in oil:
        
        oil_value.append(lines)

print(oil_value[1][0])

second_value = []
for item in oil_value:
    s = (oil_value[item].split())[-1] # It contains the thing that you need
    second_value.append(s)
print(second_value)
sns.distplot(second_value)
plt.show()

How can I get just the xx.xx number at the end of every row?

CodePudding user response:

When using numpy.ndarray, you can just use the indexing to get the last item like array[:, -1]

CodePudding user response:

It might solve your problem:

for item in arr:
    s = (arr[item].split())[-1] # It contains the thing that you need
    print(s)
# Output: 18.58
#         18.86
#         ...

If you need to convert it into the float, then you do like this:

print(float(s))
  • Related