Categories
Python Answers

How to insert multiple rows with one query with Python psycopg2?

Sometimes, we want to insert multiple rows with one query with Python psycopg2.

In this article, we’ll look at how to insert multiple rows with one query with Python psycopg2.

How to insert multiple rows with one query with Python psycopg2?

To insert multiple rows with one query with Python psycopg2, we can use the cursor mogrify method with the execute method.

For instance, we write

args_str = ','.join(cur.mogrify("(%s,%s,%s,%s,%s,%s,%s,%s,%s)", x) for x in tup)
cur.execute("INSERT INTO table VALUES " + args_str) 

to create the args_str string with the mogrify method that gets the values from the tup tuple and use them as values.

Then we concatenate that to the insert statement SQL string and call execute with the combined string.

Conclusion

To insert multiple rows with one query with Python psycopg2, we can use the cursor mogrify method with the execute method.

Categories
Python Answers

How to construct Pandas DataFrame from items in nested dictionary with Python?

Sometimes, we want to construct Pandas DataFrame from items in nested dictionary with Python.

In this article, we’ll look at how to construct Pandas DataFrame from items in nested dictionary with Python.

How to construct Pandas DataFrame from items in nested dictionary with Python?

To construct Pandas DataFrame from items in nested dictionary with Python, we can use dictionary comprehension to extract the values from the nested dicts.

And then we call from_dict with the returned dictionary to create a data frame.

For instance, we write

user_dict = {
    12: {
        "Category 1": {"att_1": 1, "att_2": "whatever"},
        "Category 2": {"att_1": 23, "att_2": "another"},
    },
    15: {
        "Category 1": {"att_1": 10, "att_2": "foo"},
        "Category 2": {"att_1": 30, "att_2": "bar"},
    },
}

pd.DataFrame.from_dict(
    {(i, j): user_dict[i][j] for i in user_dict.keys() for j in user_dict[i].keys()},
    orient="index",
)

to call from_dict with a dict that gets the values from the keys and values from each nested dict to create the data frame.

We gets the dict keys with the keys method.

Conclusion

To construct Pandas DataFrame from items in nested dictionary with Python, we can use dictionary comprehension to extract the values from the nested dicts.

And then we call from_dict with the returned dictionary to create a data frame.

Categories
Python Answers

How to find the average of a list with Python?

Sometimes, we want to find the average of a list with Python.

In this article, we’ll look at how to find the average of a list with Python.

How to find the average of a list with Python?

To find the average of a list with Python, we can use the statistics.mean method.

For instance, we write

l = [15, 18, 2, 36, 12, 78, 5, 6, 9]

import statistics

avg = statistics.mean(l)

to call statistics.mean with list l to compute the mean of the numbers in the list l.

Conclusion

To find the average of a list with Python, we can use the statistics.mean method.

Categories
Python Answers

How to get a list of values from a list of dicts with Python?

Sometimes, we want to get a list of values from a list of dicts with Python.

In this article, we’ll look at how to get a list of values from a list of dicts with Python.

How to get a list of values from a list of dicts with Python?

To get a list of values from a list of dicts with Python, we can use list comprehension.

For instance, we write

[d['value'] for d in l if 'value' in d]

to get the value of each dict d with the key 'value' from the dicts in list l.

We use if 'value' in d to ensure that the key 'value' is in the dict before we try to get the value.

Conclusion

To get a list of values from a list of dicts with Python, we can use list comprehension.

Categories
Python Answers

How to do element-wise addition of 2 lists with Python?

Sometimes, we want to do element-wise addition of 2 lists with Python

In this article, we’ll look at how to do element-wise addition of 2 lists with Python.

How to do element-wise addition of 2 lists with Python?

To do element-wise addition of 2 lists with Python, we can use the map function.

For instance, we write

from operator import add

l = list(map(add, list1, list2))

to call map with the add operator and the 2 lists that we want to do element-wise addition on.

And then we convert the iterable returned from map to a list with list.

Conclusion

To do element-wise addition of 2 lists with Python, we can use the map function.