Home > Back-end >  How to create an array from selected csv values?
How to create an array from selected csv values?

Time:11-07


array = []

for vid in account:
    vid_shares = (df.loc[df['account'] == '123', 'shares'])
    array.append(vid_shares)

print(array)

This produces something in the following format.

1      10.0
2      15.0
3       0.0
4      12.0
5      17.0
6       0.0
7       9.0
8      12.0
9      13.0
10      8.0
11     30.0
12      0.0
13     16.0
Name: shares, dtype: float64]

How would I convert this into [10, 15, 0, 12] etc so that I can use the sum function to output all the values added together?

Thanks.

CodePudding user response:

If you just want the sum.

total = df["shares"].sum()

If you want a list.

shares = df["shares"].tolist()

If you want an array.

arr = df["shares"].to_numpy()

CodePudding user response:

The sum of all the values is calculated with array.sum() e.g. in your case :

>>> array.sum()
142.0
  • Related