如何根据pandas中其他列的条件创建新列?

时间:2016-04-08 10:32:39

标签: python pandas

我有一个pandas数据框,我想在其中创建一个新列,这些值基于其他三列的值。

首先创建了一个列,并给它一个随机值300:

data['stability'] = 300

然后我设置了条件:

data['stability'][(data['wind_speed'] <= 3) & (data['clouds'] < 4 ) & (data['dagnacht'] == 'nacht')] = 6
data['stability'][(data['wind_speed'] > 3) & (data['clouds'] < 4 ) & (data['dagnacht'] == 'nacht')] = 5
data['stability'][(data['wind_speed'] >= 5) & (data['clouds'] < 4 ) & (data['dagnacht'] == 'nacht')] = 4
data['stability'][(data['wind_speed'] <= 3) & (data['clouds'] >= 4 ) & (data['dagnacht'] == 'nacht')] = 5
data['stability'][(data['wind_speed'] > 3) & (data['clouds'] >= 4 ) & (data['dagnacht'] == 'nacht')] = 4

如果检查条件是否存在,它确实告诉条件存在: 输入:

data['stability'][(data['wind_speed'] > 3) & (data['clouds'] >= 4 ) & (data['dagnacht'] == 'nacht')]

输出:

2011-08-04 21:00:00    300.0
2011-08-04 22:00:00    300.0
2011-08-04 23:00:00    300.0
2011-08-05 00:00:00    300.0
2011-08-05 01:00:00    300.0
2011-08-05 02:00:00    300.0
2011-08-05 03:00:00    300.0
2011-08-05 04:00:00    300.0
2011-08-05 05:00:00    300.0
2011-08-06 23:00:00    300.0
2011-08-07 00:00:00    300.0
2011-08-07 01:00:00    300.0

但是你可以看到它仍然具有300的值,我在开始时给它,而不是我现在想要的值4。 value_counts给我300作为唯一的值 由于某种原因,它可以读取条件,但不会将新值分配给稳定性。

我正在使用python 2.7和pandas 0.18.0

我的数据集如下所示:

           wind_speed clouds  stability dagnacht
date                                                      
2016-03-21 19:00:00        4.73      7      300.0    nacht
2016-03-21 19:10:00        4.58    NaN      300.0    nacht
2016-03-21 19:20:00        4.75    NaN      300.0    nacht
2016-03-21 19:30:00        3.67    NaN      300.0    nacht
2016-03-21 19:40:00        3.41    NaN      300.0    nacht
2016-03-21 19:50:00        3.61    NaN      300.0    nacht
2016-03-21 20:00:00        3.31      8      300.0    nacht
2016-03-21 20:10:00        3.30    NaN      300.0    nacht
2016-03-21 20:20:00        3.39    NaN      300.0    nacht
2016-03-21 20:30:00        3.59    NaN      300.0    nacht
2016-03-21 20:40:00        3.24    NaN      300.0    nacht
2016-03-21 20:50:00        2.99    NaN      300.0    nacht
2016-03-21 21:00:00        3.04      7      300.0    nacht
2016-03-21 21:10:00        3.01    NaN      300.0    nacht
2016-03-21 21:20:00        2.63    NaN      300.0    nacht
2016-03-21 21:30:00        2.41    NaN      300.0    nacht
2016-03-21 21:40:00        2.42    NaN      300.0    nacht
2016-03-21 21:50:00        2.49    NaN      300.0    nacht
2016-03-21 22:00:00        2.31      8      300.0    nacht
2016-03-21 22:10:00        2.24    NaN      300.0    nacht
2016-03-21 22:20:00        1.89    NaN      300.0    nacht
2016-03-21 22:30:00        1.88    NaN      300.0    nacht
2016-03-21 22:40:00        1.83    NaN      300.0    nacht
2016-03-21 22:50:00        1.83    NaN      300.0    nacht
2016-03-21 23:00:00        1.86      8      300.0    nacht
2016-03-21 23:10:00        2.29    NaN      300.0    nacht
2016-03-21 23:20:00        2.53    NaN      300.0    nacht
2016-03-21 23:30:00        2.36    NaN      300.0    nacht
2016-03-21 23:40:00        2.04    NaN      300.0    nacht
2016-03-21 23:50:00        1.83    NaN      300.0    nacht

提前感谢您的帮助

1 个答案:

答案 0 :(得分:1)

您正在执行chained indexing,请将您的行更改为以下格式:

data.loc[(data['wind_speed'] > 3) & (data['clouds'] >= 4 ) & (data['dagnacht'] == 'nacht'), 'stability'] = 4

因此,您要对数据进行操作而不是复制

In [19]:
data.loc[(data['wind_speed'] > 3) & (data['clouds'] >= 4 ) & (data['dagnacht'] == 'nacht'), 'stability'] = 4
data

Out[19]:
                     wind_speed  clouds  stability dagnacht
date                                                       
2016-03-21 19:00:00        4.73     7.0        4.0    nacht
2016-03-21 19:10:00        4.58     NaN      300.0    nacht
2016-03-21 19:20:00        4.75     NaN      300.0    nacht
2016-03-21 19:30:00        3.67     NaN      300.0    nacht
2016-03-21 19:40:00        3.41     NaN      300.0    nacht
2016-03-21 19:50:00        3.61     NaN      300.0    nacht
2016-03-21 20:00:00        3.31     8.0        4.0    nacht
2016-03-21 20:10:00        3.30     NaN      300.0    nacht
2016-03-21 20:20:00        3.39     NaN      300.0    nacht
2016-03-21 20:30:00        3.59     NaN      300.0    nacht
2016-03-21 20:40:00        3.24     NaN      300.0    nacht
2016-03-21 20:50:00        2.99     NaN      300.0    nacht
2016-03-21 21:00:00        3.04     7.0        4.0    nacht
2016-03-21 21:10:00        3.01     NaN      300.0    nacht
2016-03-21 21:20:00        2.63     NaN      300.0    nacht
2016-03-21 21:30:00        2.41     NaN      300.0    nacht
2016-03-21 21:40:00        2.42     NaN      300.0    nacht
2016-03-21 21:50:00        2.49     NaN      300.0    nacht
2016-03-21 22:00:00        2.31     8.0      300.0    nacht
2016-03-21 22:10:00        2.24     NaN      300.0    nacht
2016-03-21 22:20:00        1.89     NaN      300.0    nacht
2016-03-21 22:30:00        1.88     NaN      300.0    nacht
2016-03-21 22:40:00        1.83     NaN      300.0    nacht
2016-03-21 22:50:00        1.83     NaN      300.0    nacht
2016-03-21 23:00:00        1.86     8.0      300.0    nacht
2016-03-21 23:10:00        2.29     NaN      300.0    nacht
2016-03-21 23:20:00        2.53     NaN      300.0    nacht
2016-03-21 23:30:00        2.36     NaN      300.0    nacht
2016-03-21 23:40:00        2.04     NaN      300.0    nacht
2016-03-21 23:50:00        1.83     NaN      300.0    nacht

如果您尝试使用代码,则应该发出警告:

In [21]:
data['stability'][(data['wind_speed'] > 3) & (data['clouds'] >= 4 ) & (data['dagnacht'] == 'nacht')] = 4
data

C:\WinPython-64bit-3.4.3.5\python-3.4.3.amd64\lib\site-packages\IPython\kernel\__main__.py:1: 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
  if __name__ == '__main__':