python-3.6

How to insert a comma as a thousands separator in a pandas dataframe column?

|▌冷眼眸甩不掉的悲伤 提交于 2020-04-06 03:18:31
问题 I'm trying to format the Dollar Amount column to have a comma thousands separator for easier viewing, but I haven't been able to figure it out. Can someone please show me the way? import pandas as pd df = pd.read_excel('filename.xlsx') df['Dollar Amount'].head() Index Dollar Amount 0 5721.48 1 4000.00 2 4769.00 3 824.07 4 643.60 5 620.00 Name: Dollar Amount, dtype: float64 回答1: Notice it will convert your float type to object df.DollarAmount.apply(lambda x : "{:,}".format(x)) Out[509]: 0 5

How to force static typing in python [duplicate]

旧巷老猫 提交于 2020-04-05 15:06:34
问题 This question already has answers here : How to use type hints in python 3.6? (3 answers) Closed 2 years ago . Since static typing is available in Python 3.6, is it possible to force static typing for a python project or set of python files? 回答1: You can use annotations in Python3, which might help you get some benefits of static typing. However if static typing were to be completely enforced in Python, then it won't be Python anymore. It's a duck-typed dynamic language, and would loose all

Check for element without exception if element not found

女生的网名这么多〃 提交于 2020-03-25 19:25:23
问题 I am trying to probe a page for if a particular element exists on the page as it will alter a condition in my process. I use a .find_element_by_xpath to find the element and it's works fine, but if that search is performed on a page that doesn't have the element I get an exception and process halt. I am new to selenium and looked at the docs, but I just couldn't find what I'm looking for. Any guidance is appreciated. My handler: try: browser.find_element_by_xpath('//*[@id="message"]') except

Python type hinting a deque filled with myclass objects

我的未来我决定 提交于 2020-03-22 09:23:37
问题 using Python 3.6 or newer, I want to type hint a function myfunc that returns an object of MyClass. How can I hint, that myqueue is a deque filled with MyClass objects? from collections import deque global_queue = deque() class MyClass: pass def myfunc(myqueue=global_queue) -> MyClass: return myqueue.popleft() for i in range(10): global_queue.append(MyClass()) 回答1: If you are using Python 3.6.1 or higher, you can use typing.Deque: from typing import Deque from collections import deque global

Multiprocessing on Python 3 Jupyter

主宰稳场 提交于 2020-03-22 07:04:48
问题 I come here because I have an issue with my Jupiter's Python3 notebook. I need to create a function that uses the multiprocessing library. Before to implement it, I make some tests. I found a looooot of different examples but the issue is everytime the same : my code is executed but nothing happens in the notebook's interface : The code i try to run on jupyter is this one : import os from multiprocessing import Process, current_process def doubler(number): """ A doubling function that can be

Multiprocessing on Python 3 Jupyter

為{幸葍}努か 提交于 2020-03-22 07:03:29
问题 I come here because I have an issue with my Jupiter's Python3 notebook. I need to create a function that uses the multiprocessing library. Before to implement it, I make some tests. I found a looooot of different examples but the issue is everytime the same : my code is executed but nothing happens in the notebook's interface : The code i try to run on jupyter is this one : import os from multiprocessing import Process, current_process def doubler(number): """ A doubling function that can be

Adding a data file in Pyinstaller using the onefile option

若如初见. 提交于 2020-03-18 12:14:10
问题 I'm trying to add an image to the one file produced by Pyinstaller. I've read many questions/forums like this one and that one and still it's not working. I know that for one file operation, Pyinstller produce a temp folder that could be reached by sys.MEIPASS . However I don't know where exactly in my script I should add this sys.MEIPASS . Kindly show the following: 1- Where and how sys.MEIPASS should be added? In the python script, or in the spec file? 2- What is the exact command to use? I

Check a variable against Union type at runtime in Python 3.6

末鹿安然 提交于 2020-03-13 07:14:22
问题 I'm trying to write a function decorator that uses Python 3.6 type hints to check that a dictionary of arguments respects the type hints and if not raise an error with a clear description of the problem, to be used for HTTP APIs. The problem is that when the function has a parameter using the Union type I can't check a variable against it at runtime. For example, I have this function from typing import Union def bark(myname: str, descr: Union[int, str], mynum: int = 3) -> str: return descr +

Can not put single artist in more than one figure

我与影子孤独终老i 提交于 2020-02-29 07:10:31
问题 Here is my issue. I create a function that plot a list of circles. I need to plot my circle C1 first with a circle C2 then with C3.... until C40. import matplotlib.pyplot as plt from matplotlib.patches import Circle def plot_circle(Liste_circles): fig = plt.figure(figsize=(5,5)) ax = fig.add_subplot(111) # On définie un fond blanc ax.set_facecolor((1, 1, 1)) ax.set_xlim(-5, 15) ax.set_ylim(-6, 12) for c in Liste_circles: ax.add_patch(c) plt.show() Now I create C1: C1=Circle(xy=(3, 4), radius

Python subprocess.Popen doesn't take text argument

我与影子孤独终老i 提交于 2020-02-24 10:45:48
问题 According to the Python 3 documentation for subprocess.Popen, the class constructor takes an optional argument text (which is supposed to control whether the file objects stdin, stdout and stderr are opened in text mode). However, when I try setting text=true upon construction of a Popen object, I get the error Failed: TypeError: __init__() got an unexpected keyword argument 'text' and when I look in the source code (I'm using Python 3.6.4), the constructor takes no argument text . What is