0

I have a dataframe which has some column like below which contains arrays of different sizes:

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

I want to select set of array elements from each cell starting from index =1 till index = (whatever is the array length of that cell -1) and then convert the array into string in which array elements are joined using '/'

So expected output column after selecting required elements will look like :

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

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

If it's a simple array for eg which is not a dataframe i would do something like below:

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 Answer 1

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]

Use the str accessor methods:

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.