【发布时间】:2018-08-21 12:29:10
【问题描述】:
我有这段代码(有效) - 一组嵌套条件语句用于设置数据帧 (myOxides['cpx']) 的 'paragenesis1' 行中的值,具体取决于帧的其他各种行中的值。
我对python和一般编程很陌生。我在想我应该编写一个函数来执行此操作,但是如何逐元素应用该函数?这是我发现避免“系列的真值不明确”错误的唯一方法。
非常感谢任何帮助!
myOxides['cpx'].loc['paragenesis1'] = np.where(
((cpxCrOx>=0.5) & (cpxAlOx<=4)),
"GtPeridA",
np.where(
((cpxCrOx>=2.25) & (cpxAlOx<=5)),
"GtPeridB",
np.where(
((cpxCrOx>=0.5)&
(cpxCrOx<=2.25)) &
((cpxAlOx>=4) & (cpxAlOx<=6)),
"SpLhzA",
np.where(
((cpxCrOx>=0.5) &
(cpxCrOx<=(5.53125 -
0.546875 * cpxAlOx))) &
((cpxAlOx>=4) &
(cpxAlOx <= ((cpxCrOx -
5.53125)/ -0.546875))),
"SpLhzB",
"Eclogite, Megacryst, Cognate"))))
或;
df.loc['a'] = np.where(
(some_condition),
"value",
np.where(
((conditon_1) & (condition_2)),
"some_value",
np.where(
((condition_3)& (condition_4)),
"some_other_value",
np.where(
((condition_5),
"another_value",
"other_value"))))
【问题讨论】:
-
你能添加一些示例数据并尝试将其更改为最小工作示例吗?
标签: python python-3.x pandas numpy dataframe