The casting is done as shown by Patricio Moracho in his response, however, if you are free to use any library and you are going to operate with the data I recommend you consider the use of Pandas . You can install it using pip
and apart from making things much easier you will have a big difference in terms of efficiency and possibilities.
An example of produccion_diaria2.csv
:
index_plant, date, line, shift, supervisor, lbs_totales, IngUtil, shrinkage
2,2017 / 04 / 01,1,4, A, 1524,45,14
1,2017 / 05 / 01,1,5, B, 147,75.12
1,2017 / 05 / 21,1,4, C, 1478,41,14
2,2017 / 05 / 14,1,4, A, 1457,41,5
2,2017 / 05 / 04,2,4, D, 1475,14.2
We load the csv into a DataFrame:
import pandas as pd
df = pd.read_csv("produccion_diaria2.csv", parse_dates=['fecha'])
The data is converted to the appropriate type according to its compatibility automatically, although we can also indicate it explicitly. We specify that the date be explicitly peer to type datetime
.
We can see our table:
>>> df
indice_planta fecha linea turno supervisor lbs_totales IngUtil merma
0 2 2017-04-01 1 4 A 1524 45 14
1 1 2017-05-01 1 5 B 147 75 12
2 1 2017-05-21 1 4 C 1478 41 14
3 2 2017-06-14 1 4 A 1457 41 5
4 2 2017-06-04 2 4 D 1475 14 4
Now we can use all the tools that Pandas puts at our disposal to operate, filter and group data. Some very basic examples:
We only select the rows that are from May 2017:
>>> df1 = df[(df["fecha"].dt.year == 2017) & (df["fecha"].dt.month == 5)]
>>> df1
indice_planta fecha linea turno supervisor lbs_totales IngUtil merma
1 1 2017-05-01 1 5 B 147 75 12
2 1 2017-05-21 1 4 C 1478 41 14
We select the rows that have indice_planta
with value 2 and supervisor
"A":
>>> df2 = df[(df["indice_planta"] == 2) & (df["supervisor"] == "A")]
>>> df2
indice_planta fecha linea turno supervisor lbs_totales IngUtil merma
0 2 2017-04-01 1 4 A 1524 45 14
3 2 2017-06-14 1 4 A 1457 41 5
Obtain the sum of the column merma
for floor 1:
>>> df[df["indice_planta"] == 1]["merma"].sum()
26
Get a new column result of subtracting merma
to lbs_totales
:
>>> df["lbs_reales"] = df["lbs_totales"] - df["merma"]
>>> df
indice_planta fecha linea turno supervisor lbs_totales IngUtil merma lbs_reales
0 2 2017-04-01 1 4 A 1524 45 14 1510
1 1 2017-05-01 1 5 B 147 75 12 135
2 1 2017-05-21 1 4 C 1478 41 14 1464
3 2 2017-06-14 1 4 A 1457 41 5 1452
4 2 2017-06-04 2 4 D 1475 14 2 1473