Skip to main content
254 votes
5 answers
58k 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
566 votes
19 answers
284k 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
766 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: ...
851 votes
31 answers
596k 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
84k 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
4708 votes
38 answers
3.3m 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.4k
631 votes
5 answers
80k 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
63 votes
5 answers
109k 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
954 votes
8 answers
468k 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 ...
coldspeed95's user avatar
3511 votes
34 answers
299k 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
3370 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
  • 67.1k
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
984k 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
5513 votes
32 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]? Editor's notes: If your list of lists comes from a nested ...
Emma's user avatar
  • 56k
153 votes
8 answers
277k 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
14094