简体   繁体   English

多数据类型pandas Dataframe中的比较操作

[英]Comparison operation in a multi data type pandas Dataframe

I have the following pandas Dataframe:我有以下熊猫数据框:

df = pd.DataFrame({'a': [1, 2.5, 3, 'bad', 5],
                   'b': [0.1, 'good', 0.3, "ugly", 0.5],
                   'item': ['a', 'b', 'c', 'd', 'e']})
df = df.set_index('item')

As you can see, the columns have a combination of numeric and character values.如您所见,列具有数字值和字符值的组合。 I would like to change the values of the numeric values depending on the range, like for example:我想根据范围更改数值的值,例如:

0 < value <= 1, it should be replaced by "good" 0 < value <= 1,应该换成“good”

1 < value <= 2, it should be replaced by "bad" 1 < value <= 2,应该换成“bad”

2 < value <= 6, it should be replaced by "ugly" 2 < value <= 6,应该换成“丑”

Can someone help me please?有人能帮助我吗? Thanks in advance!提前致谢! The above mentioned sample dataframe consists of 2 columsn but in my actual experiment, I have about 400 columns.上面提到的示例数据框由 2 列组成,但在我的实际实验中,我有大约 400 列。 Thanks!谢谢!

Idea is convert all columns to numeric with non numeric to missing values, so is possible compare by masks and set new values with numpy.select :想法是将所有列转换为数字,将非数字转换为缺失值,因此可以通过掩码进行比较并使用numpy.select设置新值:

a = df.apply(pd.to_numeric, errors='coerce')
m1 = (a > 0) & (a <= 1)
m2 = (a > 1) & (a <= 2)
m3 = (a > 2) & (a <= 6)

arr = np.select([m1, m2, m3], ['good','bad','ugly'], default=df)

df = pd.DataFrame(arr, index=df.index, columns=df.columns)
print (df)
         a     b
item            
a     good  good
b     ugly  good
c     ugly  good
d      bad  ugly
e     ugly  good

EDIT:编辑:

df1 = pd.DataFrame({'initial': [0,1,2], 'end': [1, 2, 6], 'stg': ['good', 'bad', 'ugly']})

a = df1.apply(pd.to_numeric, errors='coerce')
m1 = (a > 0) & (a <= 1)
m2 = (a > 1) & (a <= 2)
m3 = (a > 2) & (a <= 6)

arr = np.select([m1, m2, m3], ['good','bad','ugly'], default=df1)

df = pd.DataFrame(arr, index=df1.index, columns=df1.columns)
print (df)
  initial   end   stg
0       0  good  good
1    good   bad   bad
2     bad  ugly  ugly

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

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