211,444 questions
254
votes
5
answers
57k
views
How to make good reproducible pandas examples
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 ...
765
votes
23
answers
1.1m
views
Asking the user for input until they give a valid response
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
How do I create variable variables?
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 ...
850
votes
31
answers
594k
views
How to test multiple variables for equality against a single value?
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 ...
968
votes
18
answers
83k
views
List of lists changes reflected across sublists unexpectedly
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
>...
4697
votes
38
answers
3.2m
views
How slicing in Python works
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 ...
631
votes
5
answers
79k
views
How can I pivot a dataframe? [closed]
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 ...
60
votes
5
answers
108k
views
I'm getting an IndentationError (or a TabError). How do I fix it?
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
...
949
votes
8
answers
466k
views
Pandas Merging 101
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 ...
3509
votes
34
answers
290k
views
"Least Astonishment" and the Mutable Default Argument
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:
&...
3367
votes
25
answers
2.3m
views
How do I clone a list so that it doesn't change unexpectedly after assignment?
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]
...
259
votes
10
answers
1.1m
views
How can I read inputs as numbers?
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:
...
931
votes
25
answers
979k
views
How to remove items from a list while iterating?
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 ...
5498
votes
34
answers
4.7m
views
How do I make a flat list out of a list of lists?
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 ...
152
votes
8
answers
275k
views
Selenium - wait until element is present, visible and interactable
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 ...