python - Pandas:查找空/缺失值并将其添加到 DataFrame 中

标签 python pandas row addition

我有一个数据框,其中第 1 列应该包含从 1 到 169 的所有值。如果某个值不存在,我想向我的数据框添加一个新行,其中包含所述值(和一些零)。

即使没有错误,我也无法让以下代码工作:

for i in range(1,170):
    if i in df.col1 is False:
        df.loc[len(df)+1] = [i,0,0]
    else:
        continue

有什么建议吗?

最佳答案

最好这样做:

In [37]:
# create our test df, we have vales 1 to 9 in steps of 2
df = pd.DataFrame({'a':np.arange(1,10,2)})
df['b'] = np.NaN
df['c'] = np.NaN
df
Out[37]:
   a   b   c
0  1 NaN NaN
1  3 NaN NaN
2  5 NaN NaN
3  7 NaN NaN
4  9 NaN NaN
In [38]:
# now set the index to a, this allows us to reindex the values with optional fill value, then reset the index
df = df.set_index('a').reindex(index = np.arange(1,10), fill_value=0).reset_index()
df
Out[38]:
   a   b   c
0  1 NaN NaN
1  2   0   0
2  3 NaN NaN
3  4   0   0
4  5 NaN NaN
5  6   0   0
6  7 NaN NaN
7  8   0   0
8  9 NaN NaN

所以只是解释一下上面的内容:

In [40]:
# set the index to 'a', this allows us to reindex and fill missing values
df = df.set_index('a')
df
Out[40]:
    b   c
a        
1 NaN NaN
3 NaN NaN
5 NaN NaN
7 NaN NaN
9 NaN NaN
In [41]:
# now reindex and pass fill_value for the extra rows we want
df = df.reindex(index = np.arange(1,10), fill_value=0)
df
Out[41]:
    b   c
a        
1 NaN NaN
2   0   0
3 NaN NaN
4   0   0
5 NaN NaN
6   0   0
7 NaN NaN
8   0   0
9 NaN NaN
In [42]:
# now reset the index
df = df.reset_index()
df
Out[42]:
   a   b   c
0  1 NaN NaN
1  2   0   0
2  3 NaN NaN
3  4   0   0
4  5 NaN NaN
5  6   0   0
6  7 NaN NaN
7  8   0   0
8  9 NaN NaN

如果您将循环修改为以下内容,那么它将起作用:

In [63]:

for i in range(1,10):
    if any(df.a.isin([i])) == False:
        df.loc[len(df)+1] = [i,0,0]
    else:
        continue
df
Out[63]:
   a   b   c
0  1 NaN NaN
1  3 NaN NaN
2  5 NaN NaN
3  7 NaN NaN
4  9 NaN NaN
6  2   0   0
7  4   0   0
8  6   0   0
9  8   0   0

编辑

如果您希望缺失的行出现在 df 的末尾,那么您可以创建一个临时 df,将所有值范围和其他列设置为零,然后根据 df 中缺失的值过滤此 df另一个 df 并将它们连接起来:

In [70]:

df_missing = pd.DataFrame({'a':np.arange(10),'b':0,'c':0})
df_missing
Out[70]:
   a  b  c
0  0  0  0
1  1  0  0
2  2  0  0
3  3  0  0
4  4  0  0
5  5  0  0
6  6  0  0
7  7  0  0
8  8  0  0
9  9  0  0
In [73]:

df = pd.concat([df,df_missing[~df_missing.a.isin(df.a)]], ignore_index=True)
df
Out[73]:
   a   b   c
0  1 NaN NaN
1  3 NaN NaN
2  5 NaN NaN
3  7 NaN NaN
4  9 NaN NaN
5  0   0   0
6  2   0   0
7  4   0   0
8  6   0   0
9  8   0   0

关于python - Pandas:查找空/缺失值并将其添加到 DataFrame 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28531497/

相关文章:

vba - 在 Excel Vba 中选择最后 8 行

python - Virtualenv 无法运行

Python Pandas : split and change the date format(one with eg:(aug 2018 - nov 2018)) and other with only one?

python - 将字符串/对象更改为持续时间和总和

python - append DataFrame 时为 "data type not understood"

python - 当另一行缺少数据时删除或 drop_duplicates 与 NaN 匹配所有数据

r - 如何计算 r 中的索引?

javascript - MeshLab Quadric Edge Collapse Decimation 在 JavaScript、PHP 或 Python 中保留纹理?

python - 当索引位于列表中时使用嵌套属性遍历 JSON - python

mysql - 仅在行具有相同值的情况下在 mysql 表中设置序号