在pandas-python中向不连续索引添加0/Nan行

2024-09-28 01:23:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我被一个流程困住了,我有pandas dataframe,它有一个列作为我想要的索引。在

但是这个索引有Str+num类型。在

Index_H

['N0000', 'N0002', 'N0003', 'N0007', 'N0011', 'N0017', 'N0018', 'N0020', 'N0021', 'N0023', 'N0026', 'N0027', 'N0028', 'N0030', 'N0033', 'N0034', 'N0045', 'N0050', 'N0052', 'N0055', 'N0056', 'N0057', 'N0059']

这些索引值中的每一个都有一个关联的值行,对于每个N,跨度为344列(1*344)**** 我想将其转换为连续索引,并在缺少索引的行中添加0/Nan。在


Tags: 类型dataframepandasindex流程numstrn0000
2条回答
df2 = pd.DataFrame({'foo': ['one','one','two','two','two'],
                       'bar': [ 'B', 'C', 'A', 'B', 'C'],
                       'baz': [ 2, 3, 4, 5, 6]},index=['N0001','N0004','N0005','N0006','N0009'])
idx='N'+pd.Series(list(range(1,11))).astype(str).str.zfill(4)
df2.reindex(idx)

Out[365]: 
       bar  baz  foo
N0001    B  2.0  one
N0002  NaN  NaN  NaN
N0003  NaN  NaN  NaN
N0004    C  3.0  one
N0005    A  4.0  two
N0006    B  5.0  two
N0007  NaN  NaN  NaN
N0008  NaN  NaN  NaN
N0009    C  6.0  two
N0010  NaN  NaN  NaN

使用reindex

In [350]: df
Out[350]:
       a
N0000  1
N0002  2
N0003  3
N0007  4

In [358]: df.reindex(['N%04d' % x for x in range(int(df.index[-1][1:])+1)])
Out[358]:
         a
N0000  1.0
N0001  NaN
N0002  2.0
N0003  3.0
N0004  NaN
N0005  NaN
N0006  NaN
N0007  4.0

相关问题 更多 >

    热门问题