Skip to main content

Posts

Showing posts from August, 2022

Select all numeric types from Pandas DataFrame in Python

To extract numeric types from Pandas DataFrame, Use select_types method . DataFrame.select_dtypes(include=None, exclude=None) import numpy as np import pandas as pd df = pd.DataFrame({'a': [1, 2] * 3, 'b': [True, False] * 3, 'c': [1.0, 2.0] * 3, 'd': ['one','two'] * 3}) To select all numeric types, set the parameter 'include' to np.number or 'number'. df.select_dtypes(include='number') df.select_dtypes(include=np.number) a c 0 1 1.0 1 2 2.0 2 1 1.0 3 2 2.0 4 1 1.0 5 2 2.0 You can also use the Python built-in types such as int and float, or 'int' and 'float' as string. df.select_dtypes(include=int) df.select_dtypes(include='int') a 0 1 1 2 2 1 3 2 4 1 5 2 df.select_dtypes(include=float) df.select_dtypes(include='float') c 0 1.0 1 2.0 2 1.0 3 2.0 4 1.0 5 2.0 To select strings you...

Write characters by Unicode code point in Python

Unicode code points are often written in the form of U+XXXX, where xxxx is an integer in hexadecimal. For instance character of '?' is 'U+003f' in unicode. To convert character to Unicode code point by ord() is as follow: i = ord('?') h = hex(i) print(i, h) # 62 0x3f The ord() gives digit in decimal, so to convert decimal I used the hex(). In Python an integer with the prefix 0x is a number in hexadecimal. To write '?' in unicode is as follow: print('\u003f') # ? When you write a hexadecimal Unicode code point with the prefix of \x, \u, or \U in a string literal, it is treated as that character. It should be 2, 4, or 8 digits like \xXX, \uXXXX, and \UXXXXXX, respectively.