Categories
Python Answers

How to filter dataframe rows if value in column is in a set list of values with Python Pandas?

To filter dataframe rows if value in column is in a set list of values with Python Pandas, we can use the isin method.

For instance, we write

rpt[rpt['STK_ID'].isin(stk_list)]

to call isin to get the STK_ID values that are in the stk_list list and return it.

Categories
Python Answers

How to fix UnicodeDecodeError when reading CSV file in Pandas with Pytho?

To fix UnicodeDecodeError when reading CSV file in Pandas with Python, we call Pandas read_csv with the engine argument set to 'python'.

For instance, we write

import pandas as pd
df = pd.read_csv('file_name.csv', engine='python')

to call read_csv with the file path and the engine set to 'python' to let Python decode the CSV content into the data frame df.

Categories
Python Answers

How to delete a column from a Python Pandas DataFrame?

To delete a column from a Python Pandas DataFrame, we call the drop method.

For instance, we write

df = df.drop('column_name', 1)

to call df.drop to drop the column_name column and 1 means this is the only column we drop.

Categories
Python Answers

How to convert JSON to a Python Pandas DataFrame?

To convert JSON to a Python Pandas DataFrame, we can use the json.loads and json.normalize method.

For instance, we write

from urllib2 import Request, urlopen
import json

import pandas as pd    

path1 = '52.974049,-81.205203|52.974298,-81.195755'
request=Request('http://maps.googleapis.com/maps/api/elevation/json?locations='+path1+'&sensor=false')
response = urlopen(request)
elevations = response.read()
data = json.loads(elevations)
df = pd.json_normalize(data['results'])

to get some JSON data from request.

Then we call response.read to read the JSON response into JSON.

Next, we call json.loads to load the JSON data into a dictionary.

And then we call Pandas json_normalize to load the values we want into a Pandas dataframe.

Categories
Python Answers

How to convert between datetime, Timestamp and datetime64 in Python Pandas?

To convert between datetime, Timestamp and datetime64 in Python Pandas, we can use the Timestamp function.

For instance, we write

pd.Timestamp(numpy.datetime64('2012-05-01T01:00:00.000000'))

to call Timestamp on a NumPy datetime64 value.

We can use to_datetime to convert a date time string to a Pandas date time value.

For instance, we write

pd.to_datetime('2022-05-01T01:00:00.000000+0100')

to call pd.to_datetime with a date time string to return a Python datetime value from it.