英文:
How to convert dataframe elements into conditions?
问题
我有一个数据框,想要将其中一列转换为条件。
例如,这是一个示例数据框:
df = pd.DataFrame({'a': ['>28', '27', '<26'], 'b': ['1', '2', '3']})
df
a b
0 >28 1
1 27 2
2 <26 3
我想生成一系列if语句来获取理想的b值:
if a > 28:
b = 1
elif a == 27:
b = 2
elif a < 26:
b = 3
我应该如何做呢?在我的数据框中,所有元素都以字符串形式存储。
我尝试使用iloc()
函数进行选择,但它无法处理范围条件(>
)。
有没有一种优雅的方法可以做到这一点?还是我必须手动键入所有条件?
英文:
I have a dataframe and I want to convert one column into conditions.
For example, here is a sample dataframe:
df=pd.DataFrame({'a':['>28','27','<26'],'b':['1','2','3']})
df
a b
0 >28 1
1 27 2
2 <26 3
I want to generate a series of if statements to get ideal b value:
if a > 28:
b=1
elif a=27:
b=2
elif a < 26:
b=3
How can I do that? In my dataframe, all of elements are stored as string.
I was trying to use the iloc()
function to select, but it can't deal with range conditions (>
)
If there an elegant way to do this? or do I have to manually type all the conditions?
答案1
得分: 2
如果您稍微修改您的列'a',可以使用numexpr
:
from numexpr import evaluate
a = 10
expr = 'a' + df['a'].mask(df['a'].str.isdigit(), other='==' + df['a'])
mask = expr.apply(evaluate, global_dict=globals(), local_dict=locals())
b = pd.to_numeric(df.loc[mask, 'b'], errors='coerce').head(1).squeeze() if any(mask) else None
输出:
>> b
3
>> expr
0 a>28
1 a==27
2 a<26
Name: a, dtype: object
>> mask
0 False
1 False
2 True
Name: a, dtype: bool
英文:
If you modify slightly your column 'a', you can use numexpr
:
from numexpr import evaluate
a = 10
expr = 'a' + df['a'].mask(df['a'].str.isdigit(), other='==' + df['a'])
mask = expr.apply(evaluate, global_dict=globals(), local_dict=locals())
b = pd.to_numeric(df.loc[mask, 'b'], errors='coerce').head(1).squeeze() if any(mask) else None
Output:
>>> b
3
>>> expr
0 a>28
1 a==27
2 a<26
Name: a, dtype: object
>>> mask
0 False
1 False
2 True
Name: a, dtype: bool
答案2
得分: 1
这段代码迭代DataFrame中的行,并根据列a上的条件执行操作。您可以在底部替换print语句以实现您的目标,这一点对我来说不太清楚。
import pandas as pd
df = pd.DataFrame({'a': ['>28', '27', '<26'], 'b': ['1', '2', '3']})
# 在数据框中迭代行
for index, row in df.iterrows():
a_value = row['a']
# 从条件中分离运算符和整数
if '<' in a_value or '>' in a_value:
operator = a_value[0]
num = a_value[1:]
else:
# 为等号条件添加等号
operator = '='
num = a_value
# 评估条件
condition = f'a {operator} {num}'
if eval(condition):
# 在这里执行某些操作(仅打印b以显示满足条件)
print(condition, row['b'])
英文:
This iterates over the rows in the DataFrame and performs an if condition based on column a, you can replace the print statement at the bottom with your desired goal, which was unclear to me.
import pandas as pd
df = pd.DataFrame({'a': ['>28', '27', '<26'], 'b': ['1', '2', '3']})
#Iterate over rows in dataframe
for index, row in df.iterrows():
a_value = row['a']
#Seperating operators from integers for conditions
if '<' or '>' in a_value:
operator = a_value[0]
num = a_value[1:]
else:
#adding equal sign for equal conditions
operator = '='
num = a_value
#evaluating condition
condition = f'a {operator} {num}'
if eval('condition'):
#Do something here (just printing b to show the condition is being met
print(condition, row['b'])
答案3
得分: 1
In [1]: import pandas as pd # added ------v
In [2]: df = pd.DataFrame({'a': ['>28', '==27', '<26'], 'b': ['1', '2', '3']})
In [3]: print(df)
a b
0 >28 1
1 ==27 2
2 <26 3
---
In [4]: s = "\n".join([
...: f"if a{a}:\n\tb = {b}" if idx == 0 else f"elif a{a}:\n\tb = {b}"
...: for idx, (a, b) in enumerate(zip(df["a"], df["b"]))
...: ]) # s is a multi-line string
In [5]: print(s)
if a>28:
b = 1
elif a==27:
b = 2
elif a<26:
b = 3
---
In [6]: a = 10
In [7]: exec(s)
In [8]: print(b)
3
英文:
For fun, you can try this :
In [1]: import pandas as pd # added ------v
In [2]: df = pd.DataFrame({'a': ['>28', '==27', '<26'], 'b': ['1', '2', '3']})
In [3]: print(df)
a b
0 >28 1
1 ==27 2
2 <26 3
In [4]: s = "\n".join([
...: f"if a{a}:\n\tb = {b}" if idx == 0 else f"elif a{a}:\n\tb = {b}"
...: for idx, (a, b) in enumerate(zip(df["a"], df["b"]))
...: ]) # s is a multi-line string
In [5]: print(s)
if a>28:
b = 1
elif a==27:
b = 2
elif a<26:
b = 3
In [6]: a = 10
In [7]: exec(s)
In [8]: print(b)
3
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论