如何替换缺少的列值?

时间:2019-05-27 20:01:30

标签: python pandas dataframe

我有一个数据集,该数据集的一列的前两个值显然丢失了,我无法更改! 这是数据集打印:

                 data/hora  HumExamRoom                      HumTechRoom Machine  TempExamRoom TempTechRoom
0                                NaN  2017-08-18\t09:22:33\t65.9   74034           NaN          NaN
1                                NaN  2017-10-03\t11:08:26\t60.5   74034           NaN          NaN
2  2018-02-17\t01:45:24          NaN                             69.7   74034           NaN          NaN
3  2018-02-17\t01:45:55          NaN                             67.5   74034           NaN          NaN
4  2018-02-17\t01:46:29          NaN                             65.4   74034           NaN          NaN
5  2018-02-17\t01:47:20          NaN                             63.3   74034           NaN          NaN
6  2018-02-17\t01:48:35          NaN                             61.3   74034           NaN          NaN
7  2018-02-17\t01:49:08          NaN                             63.4   74034           NaN          NaN
8  2018-02-17\t01:49:31          NaN                             65.5   74034           NaN          NaN
9  2018-02-17\t01:49:55          NaN                             67.6   74034           NaN          NaN

您会看到,对于“数据/水平”列中的第0行和第1行,没有任何值。我想用第3列中的信息替换它们。我尝试了很多事情,但都失败了。

例如:

df_74034.replace({df_74034.iloc[0,0] : '2017-08-18\t09:22:33'}, inplace=True)

然后我尝试:

df_74034.at[0, 'data/hora'] = 0

然后我得到了

A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  return super(DataFrame, self).rename(**kwargs)
/home/marlon/anaconda3/lib/python3.6/site-packages/pandas/core/frame.py:2584: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  self.loc[index, col] = value

感谢您的帮助!

3 个答案:

答案 0 :(得分:1)

您的问题是那两个包含空格(空白,制表符或空字符串)的单元格。要总体上处理它们,只需在与str.strip进行比较之前使用''。您可以直接使用loc和布尔值掩码''

进行分配
df_74034.loc[df_74034['data/hora'].str.strip().eq(''), 'data/hora'] = t.HumTechRoom


Out[1891]:
                    data/hora  HumExamRoom                 HumTechRoom  \
0  2017-08-18\t09:22:33\t65.9          NaN  2017-08-18\t09:22:33\t65.9
1  2017-10-03\t11:08:26\t60.5          NaN  2017-10-03\t11:08:26\t60.5
2        2018-02-17\t01:45:24          NaN                        69.7
3        2018-02-17\t01:45:55          NaN                        67.5
4        2018-02-17\t01:46:29          NaN                        65.4
5        2018-02-17\t01:47:20          NaN                        63.3
6        2018-02-17\t01:48:35          NaN                        61.3
7        2018-02-17\t01:49:08          NaN                        63.4
8        2018-02-17\t01:49:31          NaN                        65.5

   Machine  TempExamRoom  TempTechRoom
0    74034           NaN           NaN
1    74034           NaN           NaN
2    74034           NaN           NaN
3    74034           NaN           NaN
4    74034           NaN           NaN
5    74034           NaN           NaN
6    74034           NaN           NaN
7    74034           NaN           NaN
8    74034           NaN           NaN

答案 1 :(得分:0)

尝试:

std::cout<< "Hello" << "Mars";

答案 2 :(得分:0)

我已经解决了自己的问题。在这里:

我们无法更改数据框,因为它实际上是数据框的副本。因此,我们采用的方法不起作用-了解为什么要检查http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html

因此,我不得不直接更改df来自的数据帧df_74034。在那里,我刚刚申请了:

    df.reset_index(level=0, inplace=True) 
df_74034.rename(index=str, columns={"index": "data/hora"}, inplace=True)

然后我打印了df_74034并得到了它们的索引,如下所示:

df.loc[69333, 'index'] = '2017-08-18\t09:22:33'
df.loc[69334, 'index'] = '2017-10-03\t11:08:26' 
df.loc[69333, 'HumTechRoom'] = '65.9' 
df.loc[69334, 'HumTechRoom'] = '60.5' 

这给了我

 data/hora  HumExamRoom HumTechRoom Machine  TempExamRoom TempTechRoom
69333  2017-08-18\t09:22:33          NaN        65.9   74034           NaN          NaN
69334  2017-10-03\t11:08:26          NaN        60.5   74034           NaN          NaN
69335  2018-02-17\t01:45:24          NaN        69.7   74034           NaN          NaN
69336  2018-02-17\t01:45:55          NaN        67.5   74034           NaN          NaN
69337  2018-02-17\t01:46:29          NaN        65.4   74034           NaN          NaN
69338  2018-02-17\t01:47:20          NaN        63.3   74034           NaN          NaN
69339  2018-02-17\t01:48:35          NaN        61.3   74034           NaN          NaN
69340  2018-02-17\t01:49:08          NaN        63.4   74034           NaN          NaN
69341  2018-02-17\t01:49:31          NaN        65.5   74034           NaN          NaN
69342  2018-02-17\t01:49:55          NaN        67.6   74034           NaN          NaN

我知道应该有一种更优雅,更聪明的方式来完成我所做的事情。但这对我有用。