This question already has answers here:
Uncomfortable output of mode() in pandas Dataframe
(4 answers)
Closed 4 years ago.
train['Gender'].fillna(train['Gender'].mode()[0], inplace=True)
I got this code in one of my basic data science course. I wanted to understand, what is the significance of "[0]" after mode() in this. I would really appreciate the answer.
Thanks!
Mode documentaion
The mode() return 2 value, first is mode value second is count. So train['Gender'].mode()[0] means get the mode value of train['Gender'].
The notation [0] means that the thing before it (mode() in this case) is a collection, a list, an array, ..., and you are taking the first element.
In case you need more information, you need to include the rest of the source code (preferably by editing your question), explaining the exact meaning of the mentioned objects.
Related
This question already has answers here:
Understanding .get() method in Python [duplicate]
(5 answers)
Closed 11 months ago.
dict1={"a":4,"b":2,"A":6}
print({k.lower():dict1.get(k.lower(),0) + dict1.get(k.upper(),0) for k in dict1.keys()})
I copied this code from a youtube video. I couldn't understand the code properly. Please help me to figure it out.
I couldn't understand the purpose of 0 in second line.
k.lower():dict1.get(k.lower(),0)
I'm a beginner in python. kindly help me
The second parameter is the value parameter, that is , if the key does not exist this will be the value.
In your example, dict1["B"] does not exist. Normally that would cause an error. But because the value parameter is 0, instead of causing an error it pretends that dict1["B"] is 0. Note that this does not change the original dictionary.\
This question already has answers here:
Empty set literal?
(7 answers)
Closed 1 year ago.
I am currently learning Python and was answering questions on a practice quiz where I encountered the following questions:
What is the output of print(type({}) is set) ?
What is the output of print(type([]) is list) ?
I am unsure on how to approach the justification as to why Q1. yields False, yet Q2. yields True. I am following the guidance from a relevant post detailing the nuances of the keyword is, yet I fail to see where the two differ when comparing lists vs sets.
This is not about the nuances of is.
{} in Python is an empty dictionary. There is no literal for creating an empty set in Python (see this answer, and the actual docs). If you were to try type(set()) is set, you would produce True.
This question already has answers here:
The right way to round pandas.DataFrame?
(3 answers)
round a single column in pandas
(8 answers)
Closed 2 years ago.
I would like to check how may we use the simple basic round() function to round values in a pandas dataframe to a specific number of decimal points. I kept playing around but I couldn't get it right and I am very new to Python so am not looking for anything too sophisticated.
I understand if you use round(3.986, 2) it will simply output to 2 d.p. as 3.99.
And I know we may access the df values through df.values.
I tried exploring the df.applymap() function too.
Help !
You can use apply on the column which you want to round the numbers
>>> df['Numeric Column'].apply(lambda x : math.round(x,2))
This will give you the intended result
This question already has answers here:
Convert a list of characters into a string [duplicate]
(9 answers)
Closed 4 years ago.
I have written a code that ends up outputting what I want but in list format. Just to make it easier to understand, I will make up an input.
If I get
>>>
['H','e','l','l','o',' ','W','o','r','l','d']
as an output, how can I change it to:
>>>
'Hello World'
I have tried using .join() but it tells me that it does not work with lists as an error code.
If you need any more information, or I am being vague, just leave a comment saying so and I will update the question.
And if you leave a downvote, can you at least tell me why so that I can fix it or know what to improve for later posts
You join on the connector like this: ''.join(['H','e','l','l','o',' ','W','o','r','l','d'])
Just use join method by passing a list as parameter.
str = ''.join(['H','e','l','l','o',' ','W','o','r','l','d'])
This question already has answers here:
Understanding slicing
(38 answers)
Closed 5 years ago.
I am very new to Python and I encountered one issue that I cannot understand. Let say I have string variable:
myVar = "abcdefgh"
I want to display it backward, no problem:
print(myVar[::-1])
and I get hgfedcba. Nothing surprising here. I should get the same with this somewhat verbose code:
print(myVar[len(myVar)-1:0:-1])
but this time the result is hgfedcb. Then I have tried not to subtract 1 from len(myVar) and the result was exactly the same. I do not understand why, especially that lines:
print(myVar[::1])
print(myVar[0:len(myVar):1])
display the same results.
So, my question is why print(myVar[len(myVar):0:-1]) does not display "a"?
The verbose equivalent of print(myVar[::-1]) would be:
print(myVar[-1:-1-len(myVar):-1])
# Or
# print(myVar[len(myVar)-1:-1-len(myVar):-1])
# but this makes the the length invariant less obvious
Note that the stop parameter is exclusive, and in order to get to the actual -1, you have to additionally subtract the full length as negative indexing starts at the end of the sequence. Note also how (stop-start)*step is still the length of the slice.