简体   繁体   English

用上下限改变值

[英]Change value with upper and lower limit

I have one dataset with several column: data-pioggia-name.....我有一个包含几列的数据集:data-pioggia-name.....

I would like to rename values, within of the column pioggia.我想重命名 pioggia 列中的值。 I would like:我想:

  • values<0 = 0值<0 = 0
  • values>400 = 0值>400 = 0

I tried with:我试过:

data.loc[data.pioggia < 0]= "0"

But I have one problem with values > 400 = 0但我有一个问题,值 > 400 = 0

To recap, I am guessing your DataFrame is called data and pioggia is the name of the column that serves as filter for the values to be changed.回顾一下,我猜您的 DataFrame 被称为data ,而pioggia是用作要更改的值的过滤器的列的名称。

If so, then the following code should do:如果是这样,那么下面的代码应该做:

data.loc[(data['pioggia'] < 0) & (data['pioggia'] > 400)] = 0

Note that when you write "0" you are entering a string whose only character is 0 , not a number.请注意,当您输入"0"时,您输入的字符串的唯一字符是0 ,而不是数字。

Which is the error that you're facing for values > 400, anyway?无论如何,您面临的值 > 400 的错误是什么?

EDIT: re-reading it, I think you wanted only the values in pioggia to be changed to 0. If so, replace data.loc with data['pioggia'].loc .编辑:重新阅读它,我认为您只想将pioggia中的值更改为 0。如果是这样,请将data.loc替换为data['pioggia'].loc

I share my code with some corrections:我与一些更正分享我的代码:

import pandas as pd
data = pd.read_csv('name_dataset', sep=';')
data.info ()

data['pioggia']=data['pioggia'].fillna(0) #within dataset there is NAN
data['pioggia']=data['pioggia'].astype(int) #change value in int
data['pioggia'].loc[data.pioggia < 0]= 0 #change value<0 =0
data['pioggia'].loc[data.pioggia > 400]= 0 #change value>400 =0

# in my case I wanted to create the sum of the values, it is similar pivot table
x1_1=data.groupby('variable')[['pioggia']].sum()

or there is other solution, to get data inside interval.或者还有其他解决方案,在区间内获取数据。 Getting 0<value<400得到 0<value<400

start=0
end=400
data['pioggia']=data['pioggia'].astype(int)
data.pioggia=(data['pioggia']>start)&(data['pioggia']<=end)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM