I have a problem which I can not solve.. When I add new rows in a specific place in a datafreme, the rows after new rows disappear(it is like whe new rows step on the old ones and hide/remove them).. I take dataframe from excel file, also I use pandas.
if (first_table.iloc[first_table_row_number, 1:32].sum().all() == True):
my_row = first_table.iloc[first_table_row_num, 0:32]
final_table.iloc[final_table_row_num 91] = my_row
final_table.loc[(96 a):(96 a)] = [2023, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
final_table.loc[(97 a):(97 a)] = ['I', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
final_table.loc[(98 a):(98 a)] = ['II', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
final_table.loc[(99 a):(99 a)] = ['III', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
final_table.loc[(100 a):(100 a)] = ['IV', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
first_table - table from where i take values final_table - table in which I want put values from first_table final_table_row_num - it is index row from which I count where other rows should be first_table_row_number - indexed row from which I take values to final_table.
As you can see I only need to add 2023 in a first column, then new row 'I' in a first column and so on. Other columns should stay empty, I will add some values from first_table.
So I want to add 5 new rows to final_table, but when I do that these 5 new rows delete existing rows... Is there any solution to this? Do I need to do something with row index?
CodePudding user response:
If you want to add lines for example between line 1 and 2, then add to the index between them both - 1.5. df.loc[1.5] = ....
CodePudding user response:
You could slice and use concat to get what you want.
Example:
line = pd.DataFrame([2023, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
df2 = concat([df.iloc[:96], line, df.iloc[96:]]).reset_index(drop=True)