[英]How to add rows in pandas until condition is met
I have a dataframe like this:我有一个像这样的 dataframe:
x y frames
0 10 7
1 14 8
1 19 9
3 11 10
I want to add rows to the top of the dataframe so that frames
can start at 0 and increment up by 1 until it reaches the first original frames
value.我想在 dataframe 的顶部添加行,以便
frames
可以从 0 开始并递增 1,直到达到第一个原始frames
值。 I want to add NaN values for all other columns.我想为所有其他列添加 NaN 值。
I tried this, but it takes forever and eventually crashes:我试过这个,但它需要很长时间并最终崩溃:
starting_frame = int(df.frames.values[0])
print(starting_frame)
count = 0
while count < starting_frame:
row = ['', np.nan, np.nan, np.nan,
np.nan, np.nan, np.nan,
'', np.nan, np.nan, np.nan,
count]
d = pd.DataFrame(row)
df = pd.concat([d, df])
count+=1
print(df)
It should look like this:它应该如下所示:
x y frames
NaN NaN 0
NaN NaN 1
NaN NaN 2
NaN NaN 3
NaN NaN 4
NaN NaN 5
NaN NaN 6
0 10 7
1 14 8
1 19 9
3 11 10
Try with reindex
尝试
reindex
out = df.set_index('frames').reindex(range(df['frames'].max()+1)).reset_index()
out
Out[545]:
frames x y
0 0 NaN NaN
1 1 NaN NaN
2 2 NaN NaN
3 3 NaN NaN
4 4 NaN NaN
5 5 NaN NaN
6 6 NaN NaN
7 7 0.0 10.0
8 8 1.0 14.0
9 9 1.0 19.0
10 10 3.0 11.0
You can create a Series for the missing values of frames and use concat to preprend it to you DataFrame您可以为帧的缺失值创建一个系列,并使用 concat 将其预先设置给您 DataFrame
In [66]: pd.concat([pd.Series(range(0, df.loc[0, 'frames']), name='frames').to_frame(), df]).reset_index(drop=True)
Out[66]:
frames x y
0 0 NaN NaN
1 1 NaN NaN
2 2 NaN NaN
3 3 NaN NaN
4 4 NaN NaN
5 5 NaN NaN
6 6 NaN NaN
7 7 0.0 10.0
8 8 1.0 14.0
9 9 1.0 19.0
10 10 3.0 11.0
Problems in your code:您的代码中的问题:
pd.DataFrame(row)
, you are creating a series rather than a row in dataframe.pd.DataFrame(row)
,您将在 dataframe 中创建一个系列而不是一行。pd.concat([d, df])
and count += 1
, the bigger frames will be on top.pd.concat([d, df])
和count += 1
,较大的帧将位于顶部。 To fix these, you could use要解决这些问题,您可以使用
starting_frame = int(df.frames.values[0])
count = starting_frame - 1
while count >= 0:
row = [np.nan, np.nan, count]
d = pd.DataFrame([row], columns=['x', 'y', 'frames'])
df = pd.concat([d, df])
count -= 1
print(df)
You can also create a helper dataframe whose frames
column value starts from 0, and merge df
on that helper dataframe.您还可以创建一个助手 dataframe,其
frames
列值从 0 开始,并在该助手 dataframe 上合并df
。
df = df.merge(pd.DataFrame({'frames': range(df['frames'].max() + 1)}), how='right')
# print(df)
x y frames
0 NaN NaN 0
1 NaN NaN 1
2 NaN NaN 2
3 NaN NaN 3
4 NaN NaN 4
5 NaN NaN 5
6 NaN NaN 6
7 0.0 10.0 7
8 1.0 14.0 8
9 1.0 19.0 9
10 3.0 11.0 10
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.