Home > Blockchain >  Adding extra n rows at the end of a dataframe of a certain value
Adding extra n rows at the end of a dataframe of a certain value

Time:03-19

I have a dataframe with currently 22 rows

index value
  0     23
  1     22
  2     19
 ...
 21     20

to this dataframe, i want to add 72 rows to make the dataframe exactly 100 rows. So i need to fill loc[22:99] but with a certain value, let's say 100.

I tried something like this

uncon_dstn_2021['balance'].loc[22:99] = 100

but did not work. Any idea?

CodePudding user response:

You can do reindex

out = df.reindex(df.index.tolist()   list(range(22, 99 1)), fill_value = 100)

CodePudding user response:

You can also use pd.concat:

df1 = pd.concat([df, pd.DataFrame({'balance': [100]*(100-len(df))})], ignore_index=True)
print(df1)

# Output
     balance
0          1
1         14
2         11
3         11
4         10
..       ...
96       100
97       100
98       100
99       100

[100 rows x 1 columns]
  • Related