I have a dataset in which I need to insert a blank below based on the value in the count column. For example, if the count columns have 2, then it should insert 2 rows below. please refer to the link given below
CodePudding user response:
Something like the below should do:
import numpy as np
import pandas as pd
# Demonstration data
data = 'Number Count 12345 1 54321 3 12346 2'
data = np.array(data.split()).reshape((4,2))
df = pd.DataFrame(data[1:],columns=data[0])
# Add blank rows
df_new = pd.DataFrame()
for i, row in df.iterrows():
df_new = df_new.append(row)
for _ in range(int(row['Count'])):
df_new = df_new.append(pd.Series(), ignore_index=True)
print(df_new)
Output:
Number Count
0 12345 1
1 NaN NaN
2 54321 3
3 NaN NaN
4 NaN NaN
5 NaN NaN
6 12346 2
7 NaN NaN
8 NaN NaN