[英]Double condition in list comprehension
Suppose I am working on a pandas dataframe such as the df
one produced below:假设我正在处理一个 Pandas 数据框,例如下面生成的df
数据框:
import pandas as pd
df = pd.DataFrame([['A',3, 2000.0],
['B',4, 4502.5],
['C',5, 6250.0]],
columns=['Product', 'Number', 'Value'])
df
Product Number Value
0 A 3 2000.0
1 B 4 4502.5
2 C 5 6250.0
I can use an f-string
in order to add a column, such as:我可以使用f-string
来添加一列,例如:
df['Unit_value'] = [f'{x/3}' for x in df["Value"]]
df
This runs fine as only 1 variable x
is involved: the denominator of x/3
is constant.这运行良好,因为只涉及 1 个变量x
: x/3
的分母是常数。 Can I do something equivalent (using f-string
), but with a variable y
in denominator, y
being the Number
corresponding to the given Value
?我能做些什么等价物(使用f-string
),但有一个变量y
在分母, y
作为Number
对应于给定Value
? What I would like to have is:我想要的是:
Product Number Value Unit_Value
0 A 3 2000.0 666.66
1 B 4 4502.5 1125.63
2 C 5 6250.0 1250.00
Where: 666.66=2000.0/3 , 1125.63=4502.5/4 , 1250.00=/5
其中: 666.66=2000.0/3 , 1125.63=4502.5/4 , 1250.00=/5
I thought of something like for x in df["Value"] and y in df["Number"]
and tried to play with that, but this kind of syntax doesn't work...我想到了类似于for x in df["Value"] and y in df["Number"]
并尝试使用它,但这种语法不起作用......
You can use DataFrame.itertuples()
.您可以使用DataFrame.itertuples()
。
import pandas as pd
df = pd.DataFrame([['A',3, 2000.0],
['B',4, 4502.5],
['C',5, 6250.0]],
columns=['Product', 'Number', 'Value'])
df['Unit_value'] = [tup.Value / tup.Number for tup in df.itertuples()]
print(df)
You could also use a pure python approach with zip
.您还可以使用带有zip
的纯 python 方法。
import pandas as pd
df = pd.DataFrame([['A',3, 2000.0],
['B',4, 4502.5],
['C',5, 6250.0]],
columns=['Product', 'Number', 'Value'])
df['Unit_value'] = [val / num for num, val in zip(df['Number'], df['Value'])]
print(df)
zip
returns an iterable of tuple
s that you can then unpack into variables. zip
返回一个可迭代的tuple
,然后您可以将其解压缩为变量。
If you really want to you could do it with f strings but that would return a string and remove the ability (or at least make it more difficult) to do further calculations.如果你真的想要,你可以用 f 字符串来做,但这会返回一个字符串并删除做进一步计算的能力(或至少让它变得更加困难)。
Firstly, don't use f-strings for this, since they output strings instead of floats.首先,不要为此使用 f 字符串,因为它们输出字符串而不是浮点数。 Instead, just use the plain expressions.相反,只需使用简单的表达式。
I'm not too familiar with Pandas, but I believe the best way to do this is by treating the columns like arrays, ie do the operation on the two columns themselves:我对 Pandas 不太熟悉,但我相信最好的方法是将列视为数组,即对两列本身进行操作:
>>> df['Unit_value'] = df["Value"] / df["Number"]
>>> df
Product Number Value Unit_value
0 A 3 2000.0 666.666667
1 B 4 4502.5 1125.625000
2 C 5 6250.0 1250.000000
Otherwise, for a vanilla Python way to do it, use zip
.否则,对于普通的 Python 方式,请使用zip
。 See How to iterate through two lists in parallel?请参阅如何并行迭代两个列表?
>>> df['Unit_value'] = [x/y for x, y in zip(df["Value"], df["Number"])]
>>> df
Product Number Value Unit_value
0 A 3 2000.0 666.666667
1 B 4 4502.5 1125.625000
2 C 5 6250.0 1250.000000
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.