Skip to content

how does replace function work in python 3.x

I am new to Python, I read in a book that strings are immutable. Then how does replace() function work in Python? Does it slice up to the part where it finds the text that we want to replace and add the text with new string and then add the remaining text which is after the text we want to

NaN values in pivot_table index causes loss of data

Here is a simple DataFrame: Pivot method 1 The data can be pivoted to this: Downside: data in the 2nd row is lost because df[‘b’][1] == None. Pivot method 2 Downside: column b is lost. How can the two methods be combined so that columns b and the 2nd row are kept like so: More generally: How can i…

How can I sum the product of two list items using for loop in python?

I am trying to sum the product of two different list items in the same line using for loop, but I am not getting the output as expected. My example code: output: expected output: Answer Just zip the lists to generate pairs, multiply them and feed to sum: In above zip will return iterable of tuples containing …

Checking if a list has duplicate lists

Given a list of lists, I want to make sure that there are no two lists that have the same values and order. For instance with my_list = [[1, 2, 4, 6, 10], [12, 33, 81, 95, 110], [1, 2, 4, 6, 10]] it is supposed to return me the existence of duplicate lists, i.e. [1, 2, 4, 6, 10].