Want to turn an online CSV file into a Python Pandas data frame? Just pass a URL to read_csv: Options such as usecols and index_col work! This works on all read_* methods, including read_excel, read_json, and read_parquet.
After plotting a Python Pandas data frame in Jupyter, how do you save it to a file?
If your Python Pandas dataframe has a datetime index, use a slice to retrieve rows in a certain time period: But if the rows are unsorted? You get a KeyError. Solution:
Sorting a Python dict by value? Instead of: Use itemgetter. It’s cleaner AND ~25% faster: Sort by value, then by key:
Write to a file in Python with open(filename, ‘w’). What if filename already exists? Its contents are gone. (I hope you have backups!) Instead, try: If filename already exists, ‘x’ raises an exception. For new files, ‘x’ and ‘w’ are the same.
Retrieve rows of a Python Pandas data frame with the 6 largest values of column x: Even better: Why is it better? On a 3m row, 680 MB data frame, sort/tail took 622 ms. nlargest? Only 84 ms — 7x faster!
A Python Pandas rule of thumb: Wherever you can use a column name (string), you can use multiple column names (a list of strings):
To sort a Python Pandas data frame by column x: By x and y (ascending): By x and y, both descending: Ascending x, descending y:
Need specific column types in Python Pandas? Use select_dtypes: