Python Forum
change for loop into numpy functions
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
change for loop into numpy functions
#1
Hello,

I am having problems changing the following code including a for loop (it is running 1,5 hours by now with no solution yet) into a numpy function. Can someone help me? I could not solve it.
The issue is, that I have double Nodes with different values for tension and I need to average the tension for each node, to have an array or DataFrame just including each Node once.

'Node' values are some integers.
'Spannung' values are floats (tension values belonging to Nodes, which I want to average for same Nodes)

# next two lines are not the problem, just to understand my script maybe
df = pd.DataFrame({'Node': array[:, 0], 'Spannung': array[:, 1]})
df = df.sort_values(by=['Node'])

# last node (integer)
last_node = df["Node"].iloc[-1]

# creating empty DataFrame
df2 = pd.DataFrame({'Node':[], 'Spannung':[]})

# iterating over each Node in order to fill df2 with each node and its averaged tansion value
for i in range(1, last_node):
    meanvalue = df[df['Node'] == i].mean()
    df2.append(pd.DataFrame({'Node':[i], 'Spannung':[meanvalue.at['Spannung']]}))
I would be so happy if someone could help me!
Thank you very much,
ToffiFaye
Reply


Messages In This Thread
change for loop into numpy functions - by ToffiFaye - Feb-05-2021, 10:26 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Numpy] How to store different data type in one numpy array? water 7 771 Mar-26-2024, 02:18 PM
Last Post: snippsat
  Numpy returns "TypeError: unsupported operand type(s) for *: 'numpy.ufunc' and 'int'" kalle 2 2,689 Jul-19-2022, 06:31 AM
Last Post: paul18fr
  Change classic loop to TensorFlow tf.while_loop vsl_neuro 0 1,799 Dec-15-2020, 07:57 AM
Last Post: vsl_neuro
  "erlarge" a numpy-matrix to numpy-array PhysChem 2 3,035 Apr-09-2019, 04:54 PM
Last Post: PhysChem
  Convert indexing For Loop from MATLAB (uses numpy and pandas) bentaz 3 4,225 Mar-20-2018, 08:29 PM
Last Post: bentaz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020