The world’s leading publication for data science, AI, and ML professionals.

7 Amazing Python One-Liners You Must Know

Speedup pandas, quick data profiling, data scraping, and many more

Photo by Andrea Piacquadio from Pexels
Photo by Andrea Piacquadio from Pexels

1. Grab text from the image

pytesseract is a python library that can help us read the text from the image in a single line of python code. First thing is to read any image – that we can do using OpenCV.

Let’s install them first.

pip install opencv-python
pip install pytesseract

We need to download a .exe file to check if pytesseract is working fine in your machine.

import cv2
import pytesseract
pytesseract.pytesseract.tesseract_cmd = "YOUR_DOWNLOADED_FILE_PATH"

If the above command runs without any error then it means we are good.

Let’s load an image now and read the text in a single line of python.

img = cv2.imread("YOUR_IMAGE_PATH")
text = pytesseract.image_to_string(img)
print(text)

2. Import all the libraries in one go

pyforest is a python module that can help us import all the libraries in a single line of python. You can expect all kind of general use python libraries to get imported. This also includes most of the libraries used for machine learning tasks. This library also has some helper modules such as os, tqdm, re, and many more.

pip install pyforest

To check the list of libraries, run this dir(pyforest).

After importing pyforest, you can directly run pd.read_csv() , sns.barplot() , os.chdir() and many more.

3. Getting the data profiling report

pandas_profiling is a python library that can help us get an amazing data profiling report in a few seconds. The profiling report can be downloaded for further use.

pip install pandas_profiling

After installing the library you can import it as shown below:

import pandas_profiling
import pandas as pd

We are using pandas to import the dataset.

Importing data
Importing data
Data Snapshot
Data Snapshot
hourse_price_report=pandas_profiling.ProfileReport(df).to_file('house_report.html')

Your data report will get saved as an HTML file.

Some data report screenshots:

Overall data summary
Overall data summary
Detailed information about each variable
Detailed information about each variable
Detailed visualization for each correlation among variables
Detailed visualization for each correlation among variables

4. Merging two dictionaries

If you have two dictionaries dic1 and dic2 and you want all the key-value pair to transfer to dic1 from dic2 – the below line of code can help.

dic1.update(dic2)

5. Convert a list of strings to integers

This method is much handy when we take user input that comes as a string. The below line of python can convert your list of strings to integers.

list(map(int, ['1', '2', '3']))

Output: [1, 2, 3]

6. Scraping data from the website

Web scraping is not tough but to transform the scrapped the is a tedious task. Here, I am using a single line of python code to scrape the data about bitcoin from the Wikipedia page.

import pandas as pd
data = pd.read_html("https://en.wikipedia.org/wiki/Bitcoin")

Now we can play with the data variable to get the desired information.

7. Speed up your Pandas operations

modin is a python library – uses Ray or Dask to provide an effortless way to speed up your Pandas notebooks, scripts, and libraries.

Use the below line of code to install modin.

pip install modin

Now, you can install it like this.

import modin.pandas as pd

That’s all you need to do. No further changes in the code are required.


Conclusion

That’s all for this article. We have discussed some amazing python one-liners. Some of them are much useful like pyforest – when you have to install all the libraries in one go. And, modin library to speed up your Pandas operations like reading data frame and making any changes.

I hope you liked the article. Stay tuned!

Thanks for the reading!


Here are some of my best picks:

10 Python Tricks That Will Wow You

5 Data Science Projects that You Can Complete Over the Weekend

How a Single Mistake Wasted 3 Years of My Data Science Journey


Before you go…

If you liked this article and want to stay tuned with more exciting articles on Python & Data Science – do consider becoming a medium member by clicking here https://pranjalai.medium.com/membership.

Please do consider signing up using my referral link. In this way, the portion of the membership fee goes to me, which motivates me to write more exciting stuff on Python and Data Science.

Also, feel free to subscribe to my free newsletter: Pranjal’s Newsletter.


Related Articles