循环数据帧中的If/else语句

2024-06-16 10:53:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个有三列的数据框: 深度、页岩体积和密度。你知道吗

我需要做的是根据页岩体积和密度计算孔隙度。因此,当页岩体积大于0.7时,我将某些参数用于孔隙度计算,当页岩体积小于0.2时,我将使用其他参数。你知道吗

例如,如果页岩体积为<;0.2:

 porosity=density*2.3

如果页岩体积大于0.7:

 porosity=density*1.7

这是数据帧部分的示例,如果有:

 depth       density    VSH
 5517        2.126      0.8347083
 5517.5      2.123      0.8310949
 5518        2.124      0.8012414
 5518.5      2.121      0.7838615
 5519        2.116      0.7674243
 5519.5      2.127      0.8405414

这就是我要做的一段代码。我希望它处于for循环中,因为它将用于将来的目的:

 for index, row in data.iterrows():
     if data.loc[index, 'VSH']<0.2:
          data.loc[index,'porosity']=(data['density']*2.3)
     elif data.loc[index, 'VSH'] > 0.7:
          data.loc[index,'porosity']=(data['density']*1.7)

我得到的错误如下,如果您能为我提供帮助就太好了:

 TypeError: '<' not supported between instances of 'str' and 'float'

Tags: 数据lt示例fordata参数index体积
1条回答
网友
1楼 · 发布于 2024-06-16 10:53:14

这里iterrows是一个不好的选择,因为速度慢并且存在矢量化的解决方案,请检查Does pandas iterrows have performance issues?

所以使用^{}

m1 = data['VSH'] < 0.2
m2 = data['VSH'] > 0.7
s1 = data['density']*2.3
s2 = data['density']*1.7

data['porosity'] = np.select([m1, m2], [s1, s2])

print (data)
    depth  density       VSH  porosity
0  5517.0    2.126  0.834708    3.6142
1  5517.5    2.123  0.831095    3.6091
2  5518.0    2.124  0.801241    3.6108
3  5518.5    2.121  0.783861    3.6057
4  5519.0    2.116  0.767424    3.5972
5  5519.5    2.127  0.840541    3.6159

更好的定义是,在0.2 and 0.7之间发生什么-例如,默认参数中data['density']列的返回值:

data['porosity'] = np.select([m1, m2], [s1, s2], default=data['density'])

print (data)
    depth  density       VSH  porosity
0  5517.0    2.126  0.834708    3.6142
1  5517.5    2.123  0.831095    3.6091
2  5518.0    2.124  0.801241    3.6108
3  5518.5    2.121  0.783861    3.6057
4  5519.0    2.116  0.767424    3.5972
5  5519.5    2.127  0.840541    3.6159

相关问题 更多 >