Loop different actions for an array - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Loop different actions for an array (/thread-33043.html) |
Loop different actions for an array - Tibovdv - Mar-25-2021 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 RE: Loop different actions for an array - jefsummers - Mar-25-2021 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? RE: Loop different actions for an array - Tibovdv - Mar-25-2021 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 RE: Loop different actions for an array - shubham101194 - Mar-25-2021 how to plot histogram in pure python? RE: Loop different actions for an array - jefsummers - Mar-25-2021 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)) |