Quantcast
Viewing all articles
Browse latest Browse all 14097

split dataframe into several tables depending on condition

I am trying to split a huge dataframe into several tables but depending on conditions

import pandas as pddf=pd.DataFrame({'a':['450','455',None,'560','350',None,'580','950'],'b':['10',None,'15','12','15',None,'18','15']})print(df)currentRow = NonelastRow = Nonetables = {}  # Dictionary to store the tablestableNum = 0 # Counter for table numbersfor index, row in df.iterrows():currentRow = row['a']    if currentRow is not None and lastRow is None:               tables[tableNum] = pd.DataFrame(columns=df.columns)               tables[tableNum].loc[index] = row    elif currentRow is not None and lastRow is not None:            tables[tableNum].loc[index] = row    elif currentRow is None and lastRow is not None:        tables[tableNum].loc[index] = rowlastRow = currentRowprint('tables:', tables)

But I don't know what is wrong with this code.

Thank you for your help.

I've tried def case, if else but I don't know how to detect where the problem is


Viewing all articles
Browse latest Browse all 14097

Trending Articles