I have an array that contains numbers for example 90.01 90.02.. ,91.05, 91.06..
I want to make all the sum of 90 and device number of 90 and the same for the other numbers and put in a table how to do this in python example: 90.01 90.02=180.03/2=90.015 the same for the other numbers and put in a table how to do this in python??
I want to display only the numbers between 80-160
CodePudding user response:
The expected output is unclear, but IIUC? you can use a dictionary and conversion to integer for the keys:
l = [90.01, 90.02, 91.05, 91.06, 88.1409, 88.1409, 88.1659, 88.1659]
from collections import defaultdict
from statistics import mean
d = defaultdict(list)
for val in l:
d[int(val)].append(val)
d = {k: mean(x) for k,x in d.items()}
print(d)
output: {90: 90.015, 91: 91.055, 88: 88.1534}
CodePudding user response:
Original example that OP had provided
We can do this all using numpy
and pandas
.
Let's imagine you have this file, which we'll call table.txt
. Firstly, we need to extract the table from the file - let's use the pandas.read_csv()
from the pandas
package to automate that process a bit since we know we have whitespace delimiters (i.e. columns are separated by spaces and/or tabs):
import pandas as pd
import numpy as np
table_path = "../path/to/table.txt"
df = pd.read_csv(table_path, delim_whitespace=True)
# IF STARTS WITH HEADER INSTEAD:
# df = pd.read_csv("table.txt", header="...", delim_whitespace=True)
Now we've got our data as a DataFrame
, we can start to extract the columns we want. We know they're the first and second column from the image posted (true at the time of writing), so we can directly extract those columns from our DataFrame
and convert it to a numpy
array for processing using the DataFrame.to_numpy()
function.
columns = df.to_numpy(copy=True, dtype=np.float32)[:, 0:1] # copies data to avoid mutation (optional), slices of all the rows, only columns 0 to 1
# print(columns.shape) => (n, 2) for n rows and 2 columns
We can now create a new column of our average values and store that as a numpy
array:
# average over column axis (1) instead of row axis (0)
# we can expand dimensions from a vector to a column of shape (n, 1)
average_col = np.expand_dims(np.mean(columns, axis=1), axis=1)
If we use numpy.hstack()
we can stack them horizontally and create our table, then apply some filtering using a numpy MaskedArray
:
av_table = np.hstack([columns, average_col])
# print(table_with_average.shape) => (n, 3) for n rows and 2 columns 1 average column
filtered_table = np.ma.masked_outside(av_table, 80, 160).data
We can take this filtered table and output it to a CSV file using a DataFrame
we create from our filtered_table
array:
out_df = pd.DataFrame(filtered_table, columns=["your", "column names", "average"])
# print(out_df) => shows table in console
pd.to_csv("../path/to/output/with_average.csv", sep="\t") # \t means separated by tab characters