Categories
Python Answers

How to get indices of N maximum values in a Python NumPy array?

Sometimes, we want to get indices of N maximum values in a Python NumPy array.

In this article, we’ll look at how to get indices of N maximum values in a Python NumPy array.

How to get indices of N maximum values in a Python NumPy array?

To get indices of N maximum values in a Python NumPy array, we can use the argpartition method.

For instance, we write:

import numpy as np

a = np.array([9, 4, 4, 3, 3, 9, 0, 4, 6, 0])
ind = np.argpartition(a, -4)[-4:]
print(ind)

We call np.array with a list to create a Numpy array.

Then we call np.argpartition with a and -4 and [-4:] to get the top 4 elements in the array.

Therefore, ind is [1 5 8 0].

Conclusion

To get indices of N maximum values in a Python NumPy array, we can use the argpartition method.

Categories
Python Answers

How to apply a function to two columns of Pandas DataFrame in Python?

Sometimes, we want to apply a function to two columns of Pandas DataFrame in Python.

In this article, we’ll look at how to apply a function to two columns of Pandas DataFrame in Python.

How to apply a function to two columns of Pandas DataFrame in Python?

To apply a function to two columns of Pandas DataFrame in Python, we can use the DataFrame’s apply method.

For instance, we write:

import pandas as pd

df = pd.DataFrame({
    'ID': ['1', '2', '3'],
    'col_1': [0, 2, 3],
    'col_2': [1, 4, 5]
})
mylist = ['a', 'b', 'c', 'd', 'e', 'f']


def get_sublist(sta, end):
    return mylist[sta:end + 1]


df['col_3'] = df.apply(lambda x: get_sublist(x.col_1, x.col_2), axis=1)
print(df)

We frame the df DataFrame with some data in it.

And we have the mylist list that we want to add to DataFrame as another column.

Next, we create the get_sublist function that returns a slice of mylist.

Then we call df.apply with a function that calls get_sublist with the start and end indexes and set that as the value of the col_3 column of the DataFrame.

Therefore, df is:

  ID  col_1  col_2      col_3
0  1      0      1     [a, b]
1  2      2      4  [c, d, e]
2  3      3      5  [d, e, f]

Conclusion

To apply a function to two columns of Pandas DataFrame in Python, we can use the DataFrame’s apply method.

Categories
Python Answers

How to terminate a script with Python?

Sometimes, we want to terminate a script with Python.

In this article, we’ll look at how to terminate a script with Python.

How to terminate a script with Python?

To terminate a script with Python, we can use the sys.exit method.

For instance, we write:

import sys

sys.exit()

to exit the script.

Conclusion

To terminate a script with Python, we can use the sys.exit method.

Categories
Python Answers

How to get all subsets of a set (powerset) with Python?

Sometimes, we want to get all subsets of a set (powerset) with Python.

In this article, we’ll look at how to get all subsets of a set (powerset) with Python.

How to get all subsets of a set (powerset) with Python?

To get all subsets of a set (powerset) with Python, we can use the chain.from_iterable and combinations functions.

For instance, we write:

from itertools import chain, combinations


def powerset(iterable):
    s = list(iterable)
    return chain.from_iterable(combinations(s, r) for r in range(len(s) + 1))


p = list(powerset("abc"))
print(p)

We define the powerset function that takes an iterable object.

In the function, we convert the iterable object to a list with list.

Then we call chain.from_iterable with the list of combinations of all the elements in s.

We get all combinatins with r going from 0 to range(len(s) + 1).

Each combination is stored in a tuple.

Therefore, p is:

[(), ('a',), ('b',), ('c',), ('a', 'b'), ('a', 'c'), ('b', 'c'), ('a', 'b', 'c')]

Conclusion

To get all subsets of a set (powerset) with Python, we can use the chain.from_iterable and combinations functions.

Categories
Python Answers

How to sort a list of strings numerically with Python?

Sometimes, we want to sort a list of strings numerically with Python.

In this article, we’ll look at how to sort a list of strings numerically with Python.

How to sort a list of strings numerically with Python?

To sort a list of strings numerically with Python, we can use the int function to convert the number strings into integers.

Then we call sort on the integer array to sort the integers.

For instance, we write:

list1 = ["1", "10", "3", "22", "23", "4", "2", "200"]
list1 = [int(x) for x in list1]
list1.sort()
print(list1)

We convert each string to integers with [int(x) for x in list1], return that in an array, and assign it to list1.

Then we call list1.sort to sort the integer array in place.

Therefore, list1, is [1, 2, 3, 4, 10, 22, 23, 200].

Conclusion

To sort a list of strings numerically with Python, we can use the int function to convert the number strings into integers.

Then we call sort on the integer array to sort the integers.