我在DataFrame中有一个名为both_ntf
的列,如下所示:
column1
411.1
104.5-105.6
167.3-166.9
254
399
373.5
我的预期结果是:
column1 column2 column3
411.1 411.1 NaN
104.5-105.6 104.5 105.6
167.3-166.9 167.3 166.9
254 254 NaN
399 399 NaN
我做的if
语句似乎不起作用
if '-' in both_ntf['column1']:
print("if")
rng_ntf = both_ntf[both_ntf['column1'].str.contains("-", na=False)]
rng_ntf[['column2','column3']] =rng_ntf.column1.str.split("-",expand=True)
#Add
filtered_ntf = rng_ntf
elif '-' not in both_ntf['column1']:
print("elif")
nrng_ntf = both_ntf[~both_ntf['column1'].str.contains("-", na=False)]
nrng_ntf['column2'] = nrng_ntf['column1']
filtered_ntf = filtered_ntf.append(nrng_ntf, sort=True)
如您所见,rng_ntf
和nrng_ntf
是临时数据帧,然后附加到新的数据帧filtered_ntf
。我希望这样做更有效,更快
尝试:
您还可以指定列名
这应该可以工作,但我还没有测试它:
相关问题 更多 >
编程相关推荐