Python pandas dataframe:选择数组列中的元素集

时间:2017-11-08 01:27:01

标签: python string pandas dataframe

我有一个数据框,其中包含一些如下所示的列,其中包含不同大小的数组:

array_column
["a","b","c","d"]
["d","e","f"]
["h","i","j","k","l"]
["m","n","o","p"]
["q","r","s"]

我想从每个单元格中选择一组数组元素,从index = 1开始直到index =(无论该单元格的数组长度为-1),然后将数组转换为使用'/连接数组元素的字符串“

选择所需元素后,预期的输出列将如下所示:

array_column         
    ["b","c"]      
    ["e"]
    ["i","j","k"]
    ["n","o"]
    ["r"]

final_ouput_column         
    "b/c"     
    "e"
    "i/j/k"
    "n/o"
    "r"

如果它是一个简单的数组,例如,它不是数据帧,我会做类似下面的事情:

array = ["a","b","c","d","e","f"]
new_array = array[1:len(array)-1] // I dont know what should be done //for len(array)-1 for dataframe
print(new_array) . [output is "b","c","d","e"]
print('/'.join(new_array)) "b/c/d/e"

1 个答案:

答案 0 :(得分:1)

df

               col
0     [a, b, c, d]
1        [d, e, f]
2  [h, i, j, k, l]
3     [m, n, o, p]
4        [q, r, s]

使用str访问者方法:

df.col.str[1:-1].str.join(sep='/')

0      b/c
1        e
2    i/j/k
3      n/o
4        r
Name: col, dtype: object