Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Removing the last element of a list: Why does ".pop() not work?

I am working on a test dataset which is

print(df.head(10))

0                   NaN
1     93/2; 99/3; 05/4;
2                   NaN
3                   NaN
4                   NaN
5                   NaN

Now i want to convert the string "93/2; 99/3; 05/4;" to a more neat data structure for following analysis. Thus the first step would be so split on the ";"

df= df.apply(lambda x: x.split(';'))

which yields

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

0                        []
1    [93/2,  99/3,  05/4, ]
2                        []
3                        []
4                        []
5                        []
6                        []
7                        []
8                        []
9                        []

As you see the last element of the list is an empty value, which i want to delete. I was thinking about using the .pop() function but that yields

df = df.apply(lambda x: x.pop())
print(df.head(10))

0    
1    
2    
3    
4    
5    
6    
7    
8    
9 

if i am using slicing

df = df.apply(lambda x: x[:-1])

i get the expected output

0                      []
1    [93/2,  99/3,  05/4]
2                      []
3                      []
4                      []
5                      []
6                      []
7                      []
8                      []
9                      []

Could anyone please explain why the pop function is not working here as i expected?

Thank you in advance!

>Solution :

It works. You assign the return of pop() back into your df. pop() returns the element that gets popped. You assign that element to your df.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading