Python Forum
Loop different actions for an array
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Loop different actions for an array
#1
Hello all,

I am making a program that needs to calculate the downtime of different modules of a tool.
I have already made a part of the program where it sorts one dataframe from an array with different data frames.
The problem is that I don't know how to make a loop so it processes all the dataframes in the array automatically.

Note that I used the second value of the array (DNS_8) as I was trying to make this work for only one value.

print(df['ent_name'].unique())

#making new dataframes
df_array = []
for name in df['ent_name'].unique() :
    df_array.append(df[df.ent_name == name])
    
['DNS_8-WM2' 'DNS_8' 'DNS_8-WM1' 'DNS_8-WM5' 'DNS_8-LP1' 'DNS_8-LP2'
 'DNS_8-LP5' 'DNS_8-LP6' 'DNS_8-ATM1']

df_array[1] = df_array[1].reset_index(drop=True)
DNS_8_Sorted = df_array[1][df_array[1]['state'].isin(['IDLE_ERROR', 'PARTLY_UP', 'UP'])]
# zoek enkel naar 'IDLE_ERROR' en 'PARTLY_UP' in de kolom 'state'

DNS_8_Sorted = DNS_8_Sorted[DNS_8_Sorted['state'].ne(DNS_8_Sorted['state'].shift())]
# na een IDLE_ERROR wordt altijd een PARTLY_UP gezocht en andersom ook

m1 = DNS_8_Sorted['state'].eq('IDLE_ERROR') & DNS_8_Sorted['state'].shift(-1).eq('PARTLY_UP' or 'UP')
m2 = DNS_8_Sorted['state'].eq('PARTLY_UP' or 'UP') & DNS_8_Sorted['state'].shift().eq('IDLE_ERROR')

DNS_8_Done = DNS_8_Sorted[m1 | m2]

IDLE_ERROR = DNS_8_Done.iloc[::2]
PARTLY_UP_OR_UP = DNS_8_Done.iloc[1::2]

DNS_8_Sorted['Time_difference'] = PARTLY_UP_OR_UP['date_time_stamp'] - IDLE_ERROR['date_time_stamp'].to_numpy()
DNS_8_Sorted['Time_difference'] = DNS_8_Sorted['Time_difference'].fillna(pd.Timedelta(0))
I hope you understand my problem as my english is not so great.

Best regards,

Tibo
Reply
#2
You aren't taking advantage of the list of dataframes structure.

To reset the indices:
for my_df in df_array :
    my_df = my_df.reset_index(drop=True)
Not sure what you are wanting to do with the "sorted" as it does not appear to sort, rather filters. Is that what you want?
Tibovdv likes this post
Reply
#3
Hello Jef,

My endgoal is to make a piechart for every module of my tool.
this piechart needs to contain all the times the module went down.
However I am not sure that what I made so far is efficiƫnt coded.
The program should also be compatible with other tools that have different modules.

Kind regards

Tibo
Reply
#4
how to plot histogram in pure python?
Reply
#5
You may need to import matplotlib,
then
for my_df in df_array :
    my_df = my_df.reset_index(drop=True)
    my_df = my_df[my_df['state'].isin(['IDLE_ERROR', 'PARTLY_UP', 'UP'])]
    my_df.plot.pie(y = 'state', figsize = (6,6))
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare each element of an array in a logic statement without using a for loop leocsmith 3 2,215 Apr-01-2021, 07:57 PM
Last Post: deanhystad
  One-time actions when iterating ClassicalSoul 1 767 Apr-23-2020, 07:39 PM
Last Post: bowlofred
  Double for loop with dates in array leifeng 1 848 Apr-05-2020, 03:27 PM
Last Post: leifeng
  Loop through array items dynamically in a certain format bhojendra 3 1,342 Jun-11-2019, 03:37 AM
Last Post: micseydel
  change array column values without loop khalidreemy 2 1,697 May-05-2019, 09:05 AM
Last Post: DeaD_EyE
  N-Dim array manipulation in a loop, getting IndexError: too many indices for array cesardepaula 1 3,317 Mar-13-2019, 01:39 AM
Last Post: scidam
  Putting an array for each string that is printed to a loop ClaudioSimonetti 1 1,443 Feb-05-2019, 12:52 PM
Last Post: perfringo
  Loop for comparing 2 following datablocks in long Array Sw1p3 2 1,886 Dec-26-2017, 03:07 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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