Python commonly used method

df.select_dtypes(include=none, exclude=none)

  • Select all numeric types, use np.number or number
  • Select all strings, use object type
  • 选择datetimes, 用datetime or datetime64
  • 选择 timedeltas, 用 hour delta
  • Select pandas categorical type, use 'category' ??
    ex:
    df.select_dtypes (include = ['object'])
    Here, the selected columns are all string type columns in the dataframe

~ In Python, it means the opposite, a bit similar to sql! =
Select some rows in the DataFrame, you can use ~ to get False from True

dropna: delete the row record with null value
dropna (subset = ['xx', 'xxx']): find free worth records in the xx and xxx columns, and delete

Python-utils

  • It is a toolkit that integrates many python functions and commonly used or reusable classes.
  • When we create the code, we can also put the function or class that can be reused in utils and call it together with the main code

df.empty: returns True, if the dataframe is empty, otherwise returns False

Counter (): In Python 3, it is used to map the value of the required number
ex:

c = Counter () # Create a new empty counter
c = Counter ('abcasdf') # A counter generated by an iterative object
c = Counter ({'red': 4, 'yello': 2}) # A map generated counter
c = Counter (cats = 2, dogs = 5) # counter generated by keyword parameter
generates counter, although it is not useful here

from collections import Counter
c = Counter(‘abcasd’)
c
Counter({‘a’: 2, ‘c’: 1, ‘b’: 1, ‘s’: 1, ‘d’: 1})

c2 = Counter©
c2
Counter({‘a’: 2, ‘c’: 1, ‘b’: 1, ‘s’: 1, ‘d’: 1})

Published 69 original articles · praised 11 · 20,000+ views

Guess you like

Origin blog.csdn.net/weixin_41636030/article/details/92570270