Skip to main content
254 votes
5 answers
57k views

Having spent a decent amount of time watching both the r and pandas tags on SO, the impression that I get is that pandas questions are less likely to contain reproducible data. This is something that ...
Marius's user avatar
  • 60.6k
765 votes
23 answers
1.1m views

I am writing a program that accepts user input. #note: Python 2.7 users should use `raw_input`, the equivalent of 3.X's `input` age = int(input("Please enter your age: ")) if age >= 18: ...
566 votes
19 answers
283k views

I know that some other languages, such as PHP, support a concept of "variable variable names" - that is, the contents of a string can be used as part of a variable name. I heard that this is ...
user avatar
850 votes
31 answers
594k views

I'm trying to make a function that will compare multiple variables to an integer and output a string of three letters. I was wondering if there was a way to translate this into Python. So say: x = 0 y ...
user1877442's user avatar
  • 8,701
968 votes
18 answers
83k views

I created a list of lists: >>> xs = [[1] * 4] * 3 >>> print(xs) [[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]] Then, I changed one of the innermost values: >>> xs[0][0] = 5 >...
Charles Anderson's user avatar
4697 votes
38 answers
3.2m views

How does Python's slice notation work? That is: when I write code like a[x:y:z], a[:], a[::2] etc., how can I understand which elements end up in the slice? See Why are slice and range upper-bound ...
Simon's user avatar
  • 81.3k
631 votes
5 answers
79k views

How do I pivot the pandas dataframe below such that the col values become columns, row values become the index, and mean of val0 becomes the values? (In some cases this is called transforming from ...
piRSquared's user avatar
  • 296k
60 votes
5 answers
108k views

I have a Python script: if True: if False: print('foo') print('bar') However, when I attempt to run my script, Python raises an IndentationError: File "script.py", line 4 ...
Chris's user avatar
  • 23.2k
949 votes
8 answers
466k views

How can I perform a (INNER| (LEFT|RIGHT|FULL) OUTER) JOIN with pandas? How do I add NaNs for missing rows after a merge? How do I get rid of NaNs after merging? Can I merge on the index? How do I ...
cs95's user avatar
  • 406k
3509 votes
34 answers
290k views

def foo(a=[]): a.append(5) return a Python novices expect this function called with no parameter to always return a list with only one element: [5]. The result is different and astonishing: &...
Stefano Borini's user avatar
3367 votes
25 answers
2.3m views

While using new_list = my_list, any modifications to new_list changes my_list every time. Why is this, and how can I clone or copy the list to prevent it? For example: >>> my_list = [1, 2, 3] ...
aF.'s user avatar
  • 67k
259 votes
10 answers
1.1m views

Why are x and y strings instead of ints in the below code? (Note: in Python 2.x use raw_input(). In Python 3.x use input(). raw_input() was renamed to input() in Python 3.x) play = True while play: ...
user avatar
931 votes
25 answers
979k views

I'm iterating over a list of tuples in Python, and am attempting to remove them if they meet certain criteria. for tup in somelist: if determine(tup): code_to_remove_tup What should I ...
lfaraone's user avatar
  • 50.9k
5498 votes
34 answers
4.7m views

I have a list of lists like [ [1, 2, 3], [4, 5, 6], [7], [8, 9] ] How can I flatten it to get [1, 2, 3, 4, 5, 6, 7, 8, 9]? If your list of lists comes from a nested list ...
Emma's user avatar
  • 55.8k
152 votes
8 answers
275k views

I have a Selenium script (Python) that clicks a reply button to make the class anonemail appear. The time it takes for the class anonemail to appear varies. Because of that I have to use sleep until ...
Benjamin Arvola's user avatar

15 30 50 per page
1
2 3 4 5
14097