[英]Read CSV file to numpy array, first row as strings, rest as float
[英]How do i use numpy to return an array that sums up data from a csv file where the first column contains strings & the others contain float using
我有一個 CSV 文件,其中包含每個工廠一個月的電源,我想使用 NumPy 總結每個工廠每小時的總供應量並仍然保持尺寸。 下面是 CSV 文件中的數據示例。
PLANT 1.00hrs 2.00hrs ... 22.00hrs 23.00hrs 24.00hrs
AFAM IV - V 30.0 30.0 ... 50.0 50.0 50.0
AFAM IV - V 30.0 20.0 ... 50.0 30.0 30.0
AFAM IV - V 30.0 30.0 ... 50.0 50.0 50.0
AFAM IV - V 116.0 117.2 ... 166.1 170.6 164.6
AFAM IV - V 50.0 50.0 ... 48.0 48.0 50.0
這是我嘗試做的事情:
import pandas as pd
import numpy as np
path = 'C:\\Users\\user\\PycharmProjects\\pycharmProject\\NESI_REPORT_JAN.csv'
pdf = pd.read_csv(path)
print(np.sum(np.sum(pdf)))
這給了我以下結果:
PLANT AFAM IV - VAFAM IV - VAFAM IV - VAFAM IV - VAF...
1.00hrs 111962.9
2.00hrs 106835.2
3.00hrs 101608.21
4.00hrs 99191.9
5.00hrs 102670.56
6.00hrs 112298.41
我也試過這個:
import numpy as np
path = 'C:\\Users\\user\\PycharmProjects\\pycharmProject\\NESI_REPORT_JAN.csv'
data = np.genfromtxt(path, dtype=None, delimiter=',', names=True)
newdata = np.array(data)
print(np.sum(data, axis=0, keepdims=True))
請問我如何使用 numpy arrays 和原始尺寸對每個工廠的小時數求和。
import numpy as np
data = np.genfromtxt(fname = 'data.csv', delimiter =' ', skip_header = 1)
#skip_header -> The number of lines to skip at the beginning of the file.
data = np.nan_to_num(data, nan = 0)
#now you have a normal np.array
row_sum = np.sum(data, axis = 0)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.