我尝试查看熊猫数据帧的特定列,并尝试执行以下操作:
col0 col1 col2
int0 float0 str0
int1 float1 str0
int2 float2 str1
我试图逐行遍历该列上的数据帧,但这似乎效率不高。。lambda是一个选项,但我不确定是否有类似于DataFrame列的“列表理解”(据我所知,这是Pandas系列)。你知道吗
比如说,如果DataFrame被定义为df,那么我想做一些事情,比如:
for row in df:
if df['col2'] == str0:
# do some calculation for all str0 types
elif df['col2'] == str1:
# do another calculation for all str1 types
else:
# do another calculation for all other types (which are actually str2 types implied by the if-elif-else statement)
为了得到一个单独的NumPy矩阵,它将创建一个与DataFrame的相应行相对应的NumPy行。通过检查“col2”的条件,将根据其值执行单独的计算,并生成相应numy数组的行。你知道吗
请让我知道是否有一个有效的方法(无论是在规范,python编码以及时间和内存效率方面)!我们将不胜感激。你知道吗
您可以首先分离出与
col2
列的每个值对应的行。你知道吗这更像是一个
np.select
问题从维护角度来看,最简单的事情是创建一个函数,例如:
然后将其应用于您的专栏:
相关问题 更多 >
编程相关推荐