你可以使用循环来基于条件在Pandas数据框中进行列的子集化。

huangapple go评论92阅读模式
英文:

How do I subset columns in a Pandas dataframe based on criteria using a loop?

问题

I can help you with the translation of the provided code snippet. Here is the translation:

我有一个名为bag的Pandas数据帧其中包含名为beans1beans2和beans3的列
```python
bag = pd.DataFrame({'beans1': [3,1,2,5,6,7], 'beans2': [2,2,1,1,5,6], 'beans3': [1,1,1,3,3,2]})
bag
Out[50]: 
   beans1  beans2  beans3
0       3       2       1
1       1       2       1
2       2       1       1
3       5       1       3
4       6       5       3
5       7       6       2
我想要使用循环来筛选每一列中大于1的观测值以便得到以下结果
beans1
0       3
2       2
3       5
4       6
5       7

   beans2
0       2
1       2
4       5
5       6

   beans3
3       3
4       3
5       2
手动执行的方法是
beans1=beans.loc[bag['beans1']>1,['beans1']]
beans2=beans.loc[bag['beans2']>1,['beans2']]
beans3=beans.loc[bag['beans3']>1,['beans3']]
但我需要使用循环类似于
for i in range(1,4):
    beans+str(i).loc[beans.loc[bag['beans'+i]>1,['beans'+str(i)]]
但这并没有起作用我需要Python版本的R的eval(parse(text=""))
任何帮助都将不胜感激非常感谢

Please note that the code snippets you provided have some issues, and it seems like there might be errors in your code. If you have specific questions or need assistance with the code, please feel free to ask.

英文:

I have a Pandas dataframe called "bag' with columns called beans1, beans2, and beans3

bag = pd.DataFrame({'beans1': [3,1,2,5,6,7], 'beans2': [2,2,1,1,5,6], 'beans3': [1,1,1,3,3,2]}) 
bag
Out[50]: 
   beans1  beans2  beans3
0       3       2       1
1       1       2       1
2       2       1       1
3       5       1       3
4       6       5       3
5       7       6       2

I want to use a loop to subset each column with observations greater than 1, so that I get:

beans1
0       3
2       2
3       5
4       6
5       7

   beans2
0       2
1       2
4       5
5       6

   beans3
3       3
4       3
5       2

The way to do it manually is :

beans1=beans.loc[bag['beans1']>1,['beans1']]
beans2=beans.loc[bag['beans2']>1,['beans2']]
beans3=beans.loc[bag['beans3']>1,['beans3']]

But I need to employ a loop, with something like:

for i in range(1,4):
    beans+str(i).loc[beans.loc[bag['beans'+i]>1,['beans'+str(i)]]

But it didn't work. I need a Python version of R's eval(parse(text="")))
Any help appreciated. Thanks much!

答案1

得分: 1

以下是您要翻译的内容:

使用global变量是可能的,但不被推荐,详情请参考此链接

for i in range(1, 4):
    globals()['beans' + str(i)] = bag.loc[bag['beans'+str(i)] > 1, ['beans'+str(i)]]

for c in bag.columns:
    globals()[c] = bag.loc[bag[c] > 1, [c]]

print(beans1)
   beans1
0       3
2       2
3       5
4       6
5       7

更好的做法是创建字典:

d = {c: bag.loc[bag[c] > 1, [c]] for c in bag}

print(d['beans1'])
   beans1
0       3
2       2
3       5
4       6
5       7
英文:

It is possible, but not recommended, with globals:

for i in range(1,4):
    globals()['beans' + str(i)] = bag.loc[bag['beans'+str(i)]>1,['beans'+str(i)]]

for c in bag.columns:
    globals()[c] = bag.loc[bag[c]>1,[c]]
    
print (beans1)
   beans1
0       3
2       2
3       5
4       6
5       7

Better is create dictionary:

d = {c: bag.loc[bag[c]>1, [c]] for c in bag}

print (d['beans1'])
   beans1
0       3
2       2
3       5
4       6
5       7

huangapple
  • 本文由 发表于 2020年1月3日 17:10:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/59575761.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定