Home:ALL Converter>Python pandas dataframe : selecting set of elements in array column

Python pandas dataframe : selecting set of elements in array column

Ask Time:2017-11-08T09:27:00         Author:Nikita Gupta

Json Formatter

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"

Author:Nikita Gupta,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/47170100/python-pandas-dataframe-selecting-set-of-elements-in-array-column
yy