我想使用apply轻松地将我的列分割成两个(或更多)。(我可以像使用here那样使用split,但是有些异常很难处理。This答案类似,但同样输出一列。
import pandas as pd
df = pd.DataFrame({"xVal":[1,2,7,4], "xRef":["1-2","2-3",">4", "NoReference"]})
def sep_ref(row):
if '-' in row:
return row.split("-")
else:
# handle and return some list
return [row, row]
# broken assinment
df['xlow'], df['xhigh'] = df.xRef.apply(sep_ref)df
xVal xRef
0 1 '1-2'
1 2 '2-3'
2 7 '>4'
3 4 'NoReference'期望输出
xVal xlow xhigh
0 1 1 2
1 2 2 3
2 7 4 NaN
3 4 NaN NaN简单的解决方案是运行两个单独的应用函数,但这不那么优雅,可能会使异常处理更加困难。是否有一种方法一次追加2列与应用?
发布于 2019-08-08 03:55:59
更新:
我刚刚注意到NaN的首选项。下面是一个解决办法:
import pandas as pd
import numpy as np
df = pd.DataFrame({"xVal":[1,2,7,4], "xRef":["1-2","2-3",">4", "NoReference"]})
def sep_ref(row):
if '-' in row:
return [int(x) for x in row.split("-")]
elif row.startswith('>'):
return [int(row[1:]), np.nan]
elif row.startswith('<'):
return [np.nan, int(row[1:])]
else:
return [np.nan, np.nan]
# not broken assinment
df['xlow'] = None
df['xhigh'] = None
df[['xlow', 'xhigh']] = [*df.xRef.apply(sep_ref)]
print(df) xVal xRef xlow xhigh
0 1 1-2 1.0 2.0
1 2 2-3 2.0 3.0
2 7 >4 4.0 NaN
3 4 NoReference NaN NaN原版:
为此,我认为您需要首先初始化"xlow“和"xhigh”列。
# not broken assignment
df['xlow'] = None
df['xhigh'] = None
df[['xlow', 'xhigh']] = [*df.xRef.apply(sep_ref)]
print(df)输出:
xVal xRef xlow xhigh
0 1 1-2 1 2
1 2 2-3 2 3
2 7 >4 >4 >4
3 4 NoReference NoReference NoReferencehttps://stackoverflow.com/questions/57404910
复制相似问题