Categories
Python Answers

How to GroupBy results to dictionary of lists with Python Pandas?

Sometimes, we want to GroupBy results to dictionary of lists with Python Pandas.

In this article, we’ll look at how to GroupBy results to dictionary of lists with Python Pandas.

How to GroupBy results to dictionary of lists with Python Pandas?

To GroupBy results to dictionary of lists with Python Pandas, we can use the call to_dict after calling groupby.

For instance, we write

d = df.groupby('Column1')['Column3'].apply(list).to_dict()

to call groupby to group the rows by values in the Column1 column.

And then we get the grouped values in the Column3 column.

Next, we call apply with list to combine the grouped value into a list.

And then we call to_dict to return a dict with the keys being the group value and the values being a list of the items in the group.

Conclusion

To GroupBy results to dictionary of lists with Python Pandas, we can use the call to_dict after calling groupby.

Categories
Python Answers

How to fix PermissionError: [Errno 13] Permission denied with Python open?

To fix PermissionError: [Errno 13] Permission denied with Python open, we should make sure the path we call open with is a file.

For instance, we write

import os

path = r"/path/to/file.txt"
assert os.path.isfile(path)
with open(path, "r") as f:
    pass

to make sure that the path is a path to a file with os.path.isfile before we call open to open the file at the path.

Categories
Python Answers

How to load default profile in Chrome using Python Selenium Webdriver?

Sometimes, we want to load default profile in Chrome using Python Selenium Webdriver.

In this article, we’ll look at how to load default profile in Chrome using Python Selenium Webdriver.

How to load default profile in Chrome using Python Selenium Webdriver?

To load default profile in Chrome using Python Selenium Webdriver, we can use the ChromeOptions object.

For instance, we write

from selenium import webdriver

options = webdriver.ChromeOptions() 
options.add_argument("user-data-dir=C:\\Path")
w = webdriver.Chrome(executable_path="C:\\Users\\chromedriver.exe", chrome_options=options)

to create a ChromeOptions object.

And then we call add_argument to set some options for Chrome.

Next, we create a Chrome object with the executable_path of the Chrome driver and the options ChromeOptions object to set the options when running Chrome.

Conclusion

To load default profile in Chrome using Python Selenium Webdriver, we can use the ChromeOptions object.

Categories
Python Answers

How to apply function to each element of a list with Python?

Sometimes, we want to apply function to each element of a list with Python.

In this article, we’ll look at how to apply function to each element of a list with Python.

How to apply function to each element of a list with Python?

To apply function to each element of a list with Python, we use the map function.

For instance, we write

from string import upper

mylis = ['this is test', 'another test']
mapped = map(upper, mylis)

to create the mylis with some strings inside.

Then we call map to call the upper function on each entry of mylis.

An iterator is then returned with the mylis string in upper case from map.

Conclusion

To apply function to each element of a list with Python, we use the map function.

Categories
Python Answers

How to get text with Selenium WebDriver in Python?

Sometimes, we want to get text with Selenium WebDriver in Python.

In this article, we’ll look at how to get text with Selenium WebDriver in Python.

How to get text with Selenium WebDriver in Python?

To get text with Selenium WebDriver in Python, we use the element’s text property.

For instance, we write

driver.find_element_by_class_name("ctsymbol").text

to call find_element_by_class_name to get the first element with class ctsymbol.

And then we use the text property of it to get the text of the element returned.

Conclusion

To get text with Selenium WebDriver in Python, we use the element’s text property.