Pandas 熊猫按特定增量在间隔中添加值

Pandas 熊猫按特定增量在间隔中添加值,pandas,Pandas,有没有办法使用pandas函数按特定增量添加值/行 例如: 这就是我所拥有的: df = pd.DataFrame([1.1,2,2.8]) df value other1 other2 zebra 0.3 250 bunny 0.7 10 rat 1.0 35 cat 1.1 100 dog 2.0 150 mouse 2.8 125 编辑1: 这就是我想要的,理想情况下,插入行的

有没有办法使用pandas函数按特定增量添加值/行

例如: 这就是我所拥有的:

df = pd.DataFrame([1.1,2,2.8])
df
      value other1  other2 
zebra  0.3           250
bunny  0.7           10
rat    1.0           35
cat    1.1   100
dog    2.0   150
mouse  2.8   125
编辑1:

这就是我想要的,理想情况下,插入行的索引是最简单的,但保留以前的行名称。

df_goal = pd.DataFrame([1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9,2,2.1,2.2,2.3,2.4,2.5,2.6,2.7,2.8])
df_goal
      value  other1   other2
zebra  0.3             250
1      0.4
2      0.5
3      0.6
bunny  0.7             10
5      0.8
6      0.9
rat    1.0             35
cat    1.1    100
1      1.2
2      1.3
3      1.4
4      1.5
5      1.6
6      1.7
7      1.8
8      1.9
dog    2.0   150
10     2.1
11     2.2
12     2.3
13     2.4
14     2.5
15     2.6
16     2.7
mouse  2.8   125
编辑2:

另外,我想保留以前存在的
其他
列的值,任何新行都是空的或零。

我认为您可以通过以下方式使用:


我认为您可以通过以下方式使用:



这在很大程度上是可行的,但我丢失了以前存在的索引名。我想保留它们,我用这个事实更新了我的问题以使其更清楚。另外,刚刚注意到,生成的数据框使我的数据框中的所有其他列都变得更复杂。给我一些时间。请检查编辑后的答案,我再添加一列。重新编制索引后,您将得到
NaN
s,我将它们替换为
ffill
。可以吗?或者可以用标量替换,比如
df=df.reindex(a,公差=0.05,方法为'nearest',填充值=0)。reset_index()。set_index('index')
很抱歉,我到目前为止还没有机会查看这个。您的解决方案与我想要的非常接近。我不想像您使用
ffill
那样重复其他列。我再次编辑了我的问题以获得预期的输出。这在很大程度上起作用,但我丢失了以前存在的索引名。我想保留它们,我用这个事实更新了我的问题以使其更清楚。此外,刚刚注意到,生成的数据框使我的数据框中的所有其他列变得更复杂。给我一些时间。请检查编辑后的答案,我再添加一列。重新编制索引后,您将得到
NaN
s,我将它们替换为
ffill
。可以吗?或者可以用标量替换,比如
df=df.reindex(a,公差=0.05,方法为'nearest',填充值=0)。reset_index()。set_index('index')
很抱歉,我到目前为止还没有机会查看这个。您的解决方案与我想要的非常接近。我不想像您使用
ffill
那样重复其他列。我再次编辑了我的问题以获得预期的结果。
#create index by value column
df = df.reset_index().set_index('value')

#reindex floatindex
s = 0.1
a = np.arange(df.index.min(),df.index.max() + s, step=s)
df = df.reindex(a, tolerance=s/2., method='nearest')

#replace NaN in another columns as index
cols = df.columns.difference(['index'])
df[cols] = df[cols].fillna('')

#replace NaN by range
s = pd.Series(np.arange(len(df.index)), index=df.index)
df['index'] = df['index'].combine_first(s)

#swap column with index
df = df.reset_index().set_index('index')
print (df)

       value other1 other2
index                     
zebra    0.3           250
1        0.4              
2        0.5              
3        0.6              
bunny    0.7            10
5        0.8              
6        0.9              
rat      1.0            35
cat      1.1    100       
9        1.2              
10       1.3              
11       1.4              
12       1.5              
13       1.6              
14       1.7              
15       1.8              
16       1.9              
dog      2.0    150       
18       2.1              
19       2.2              
20       2.3              
21       2.4              
22       2.5              
23       2.6              
24       2.7              
mouse    2.8    125