Pandas Insert Empty Row At 0th Position
Suppose have following data frame A B 1 2 3 4 5 4 5 6 7 8 I want to check if df(0,0) is nan then insert pd.series(np.nan) at 0th position. So in above
Solution 1:
Use append
of DataFrame
with one empty row:
df1 = pd.DataFrame([[np.nan]* len(df.columns)], columns=df.columns)
df = df1.append(df, ignore_index=True)
print (df)
A B C D E
0NaNNaNNaNNaNNaN11.02.03.04.05.024.05.06.07.08.0
Solution 2:
Perhaps you can first append a row with zeros, shift the whole rows and overwrite the first with 0:
df
A B C D E
0 1 2 3 4 5
1 4 5 6 7 8
df.loc[len(df)] = 0
df
A B C D E
0 1 2 3 4 5
1 4 5 6 7 8
2 0 0 0 0 0
df = df.shift()
df.loc[0] = 0
df
A B C D E
0 0.0 0.0 0.0 0.0 0.0
1 1.0 2.0 3.0 4.0 5.0
2 4.0 5.0 6.0 7.0 8.0
Post a Comment for "Pandas Insert Empty Row At 0th Position"