根据单个分隔符将多个值分为两列

时间:2019-12-23 12:06:08

标签: python-3.x pandas postgresql

我是熊猫新手,我有一种情况想将长度列分为a和b两列.length列中的值是成对的。我想比较第一对较小的值应该在b中的较大值中然后比较同一行中的下一对,a中较小,b中较大。

我有一百行,我认为我不能使用str.split,因为有多个值和相同的定界符,我不知道该怎么做 输出应该是这样的。 任何帮助将不胜感激

length                                            a                         b
{22.562,"35.012","25.456",37.342,24.541,38.241}   22.562,25.45624.541    35.012,37.342,38.241
{21.562,"37.012",25.256,36.342}                   31.562,25.256           37.012,36.342
{22.256,36.456,26.245,35.342,25.56,"36.25"}     22.256,26.245,25.56      36.456,35.342,36.25

我尝试过

df['a'] = df['length'].str.split(',').str[0::2]
df['b'] = df['length'].str.split(',').str[1::3]

通过该ode列b的输出是完美的,但是col a先打印完整对,然后再打印第二对。它不只给出第0、2、4个值

1 个答案:

答案 0 :(得分:0)

问题来自您的长度列是由集合而不是列表组成的事实。

这是一种通过将长度列转换为列表来完成所需操作的方法:

df['length'] = [list(x) for x in df.length] # We cast the sets as lists
df['a'] = [x[0::2] for x in df.length]
df['b'] = [x[1::2] for x in df.length]

输出:

                                             length                         a  \
0  [35.012, 37.342, 38.241, 22.562, 24.541, 25.456]  [35.012, 38.241, 24.541]   
1                  [25.256, 36.342, 21.562, 37.012]          [25.256, 21.562]   
2    [35.342, 36.456, 36.25, 22.256, 25.56, 26.245]    [35.342, 36.25, 25.56]   

                          b  
0  [37.342, 22.562, 25.456]  
1          [36.342, 37.012]  
2  [36.456, 22.256, 26.245]