F
F
frasero2021-12-03 22:18:38
Python
frasero, 2021-12-03 22:18:38

How to leave all the repetitions of the second column, in a single variant, while choosing the maximum element from the first column as a pair?

There is such not the most beautiful code:

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
print()
x1 = pd.read_excel("start.xlsx", sheet_name = "x1").to_numpy()
x2 = pd.read_excel("start.xlsx", sheet_name = "x2").to_numpy()

resch = []
resd = []
for i in range(len(x2)):
    for j in range(len(x1)):
        resch.append(min(x1[j][0],x2[i][0]))
        resd.append(round(x1[j][1] + x2[i][1],2))
print(resch)
print("------------------------------")
print(resd)
print("------------------------------")
result = np.array([resch,resd])
result = result.T
print(result)


As a result, we get the following array:

61aa6d11f21e8334307610.png

How to leave all the repetitions of the second column, in a single variant, while choosing the maximum element from the first column as a pair?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
aRegius, 2021-12-03
@aRegius

pandas:

result = pd.DataFrame(result)
pd_result = result.groupby(1, as_index=False).max().reindex(columns=[0, 1])
np_result = pd_result.to_numpy()

Python:
from collections import defaultdict

data = defaultdict(list)
for value, key in result:
    data[key].append(value)

result = [[max(value), key] for key, value in data.items()]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question