英文:
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数据帧,其中包含名为beans1、beans2和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 global
s:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论