将列的百分比设置为0( pandas ) [英] Set percentage of column to 0 (pandas)

查看:88
本文介绍了将列的百分比设置为0( pandas )的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个pandas数据框,我想将某列的百分比设置为0.假设df有两列.

I have a pandas data frame and I want to set some percentage of a column to 0. Let's say the df has two columns.

  A   B  
  1   6
  2   7
  3   8
  4   4
  5   9

我现在想将df的前20%和后20%的B设置为0.

I now want to set B for the first and last 20 % of the df to 0.

  A   B  
  1   0
  2   7
  3   8
  4   4
  5   0

推荐答案

使用

Use numpy.r_ for join first and last positions and then change values by iloc, for position of column B use Index.get_loc:

N = .2
total = len(df.index)
#convert to int for always integer
i = int(total * N)
idx = np.r_[0:i, total-i:total]
df.iloc[idx, df.columns.get_loc('B')] = 0

或者:

N = .2
total = len(df.index)
i = int(total * N)
pos = df.columns.get_loc('B')

df.iloc[:i, pos] = 0
df.iloc[total - i:, pos] = 0


print (df)
   A  B
0  1  0
1  2  7
2  3  8
3  4  4
4  5  0

如果 Sparsedataframe 且具有相同类型的值可以转换为numpy数组,设置值并转换回:

If Sparsedataframe and same type of values is possible convert to numpy array, set value and convert back:

arr = df.values
N = .2
total = len(df.index)
i = int(total * N)
pos = df.columns.get_loc('B')
idx = np.r_[0:i, total-i:total]

arr[idx, pos] = 0
print (arr)
[[1 0]
 [2 7]
 [3 8]
 [4 4]
 [5 0]]

df = pd.SparseDataFrame(arr, columns=df.columns)
print (df)
   A  B
0  1  0
1  2  7
2  3  8
3  4  4
4  5  0

print (type(df))
<class 'pandas.core.sparse.frame.SparseDataFrame'>

另一种解决方案是先转换为密集,然后转换回:

Another solution is first convert to dense and then convert back:

df = df.to_dense()
#apply solution
df = df.to_sparse()

这篇关于将列的百分比设置为0( pandas )的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆