Indexing Pandas DataFrame using variable [duplicate] - python

This question already has answers here:
Python slice how-to, I know the Python slice but how can I use built-in slice object for it?
(6 answers)
Closed 6 years ago.
I'm just wondering if I can do something like:
df.loc['1990':'2000']
by doing something like:
my_slice = '1990':'2000'
df.loc[my_slice]
What I've written doesn't work, but is there something similar that does?

Yes, but you don't write slices like that. You write slice('1900', '2000', None) instead.

Related

Python List as List Index [duplicate]

This question already has answers here:
Unpacking tuples/arrays/lists as indices for Numpy Arrays
(4 answers)
Closed 3 years ago.
Really not sure the right question to ask for this, but is it possible to have a list as the index of a list?
Ex:
pixelAddr=[50,50] # list
img[pixelAddr[0], pixelAddr[1]]=[255,255,255] # This is the way I know
# Is something like this possible? I get syntax errors when I try it...
img[*pixelAddr]=[255,255,255]
Btw, using python 3.7
when you do: img[pixelAddr[0], pixelAddr[1]] you are actually just re-packing the indices as a tuple so that is really all you need:
pixelAddr=(50,50) # NOTE THESE ARE ROUND PARENTHASIS
img[pixelAddr]=[255,255,255]
# or
pixelAddrList = [50,50]
img[tuple(pixelAddr)]=[255,255,255]

Python: one variable but different values? [duplicate]

This question already has answers here:
Why does adding a trailing comma after an expression create a tuple?
(6 answers)
What does __all__ mean in Python?
(10 answers)
Closed 5 years ago.
I have a line of code from Python forbidden fruit module:
__all__ = 'curse', 'curses', 'reverse'
I know what strings are and I know what arrays and tuples are. What kind of variable is this? How can we use this and for what?
It's a tuple. If you want to find out the type of something, use the type function - e.g.
type(__all__)

C command similar to "if x in list" of Python [duplicate]

This question already has answers here:
Check if a value exists in an array in Cython
(2 answers)
Closed 6 years ago.
In Python we could use if x in list: so I was wondering if there's a similar command in C, so that we don't have to go through the whole thing using a for.
How can you know whether a value is contained in an array without cycling through it? This is exactly what Python does under the hood. No, there's no magical way to instantly know this.

what's the meaning of "[ ]" in python references? [duplicate]

This question already has answers here:
What do square brackets, "[]", mean in function/class documentation?
(4 answers)
Closed 7 years ago.
I'm using python for a long time. but it was always strange to me why In python references, commands are written like this:
del var1[,var2[,var3[....,varN]]]]
I know that if I want to use the above command I should write it in this way:
del var1, var1
I can't understand the meaning of [] is it related to the lists? any help will be appreciated.
Its just showing that these parameters are optional. This is normal style for references.
There is nothing to do with this in python. Just a tutorial explanation.
I bealive, this is taken origin from Usage message

Python += with a list and a tuple [duplicate]

This question already has answers here:
Why does += behave unexpectedly on lists?
(9 answers)
Why can't I add a tuple to a list with the '+' operator in Python?
(3 answers)
Closed 7 years ago.
I saw someone wrote an interesting python line online, but couldn't understand why it works. So we can try the following lines in python interpreter:
s=[1]
s=s+(1,-1)
This will result in an error "TypeError: can only concatenate list (not "tuple") to list". But if done in another way:
s=[1]
s+=(1,-1)
will result in s = [1,1,-1]
So I used to thought x=x+y is equivalent to x+=y, can someone tell me how they are different and why the second way works? Thanks in advance.
Instead of += use list.extend:
s = [1]
s.extend((1,-1))

Categories

Resources