使用查询将np.nans分配给Pandas列的行
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用查询将np.nans分配给Pandas列的行相关的知识,希望对你有一定的参考价值。
我想在满足某些条件时将NaN分配给Pandas数据框中的列的行。
有关可重现的示例,有些数据:
'"Price":"1581292800000":21.6800003052,"1581379200000":21.6000003815,"1581465600000":21.6000003815,"1581552000000":21.6000003815,"1581638400000":22.1599998474,"1581984000000":21.9300003052,"1582070400000":22.0,"1582156800000":21.9300003052,"1582243200000":22.0200004578,"1582502400000":21.8899993896,"1582588800000":21.9699993134,"1582675200000":21.9599990845,"1582761600000":21.8500003815,"1582848000000":22.0300006866,"1583107200000":21.8600006104,"1583193600000":21.8199996948,"1583280000000":21.9699993134,"1583366400000":22.0100002289,"1583452800000":21.7399997711,"1583712000000":21.5100002289,"Target10":"1581292800000":22.9500007629,"1581379200000":23.1000003815,"1581465600000":23.0300006866,"1581552000000":22.7999992371,"1581638400000":22.9599990845,"1581984000000":22.5799999237,"1582070400000":22.3799991608,"1582156800000":22.25,"1582243200000":22.4699993134,"1582502400000":22.2900009155,"1582588800000":22.3248996735,"1582675200000":null,"1582761600000":null,"1582848000000":null,"1583107200000":null,"1583193600000":null,"1583280000000":null,"1583366400000":null,"1583452800000":null,"1583712000000":null'
在这个特定的玩具示例中,当“ Target10”列具有NaN时,我想将NaN分配给“价格”列。 (一般情况下,条件可能会更复杂)
此行代码实现了该特定目标:
toy_data.Price.where(toy_data.Target10.notnull(), toy_data.Target10)
但是,当我尝试使用查询并将NaN分配给目标列时,我失败了:
toy_data.query('Target10.isnull()', engine = 'python').Price = np.nan
以上行完整保留toy_data
。>>
为什么?我应该如何使用query
替换特定行中的值?
我想在满足某些条件时将NaN分配给Pandas数据框中的列的行。对于可重现的示例,下面是一些数据:'“ Price”:“ 1581292800000”:21.6800003052,“ ...
答案
一种方法是-
以上是关于使用查询将np.nans分配给Pandas列的行的主要内容,如果未能解决你的问题,请参考以下文章
Pandas 将具有多个值的行数据合并到列的 Python 列表中