Categories
Python Answers

How to get the item frequency count in Python?

Sometimes, we want to get the item frequency count in Python.

In this article, we’ll look at how to get the item frequency count in Python.

How to get the item frequency count in Python?

To get the item frequency count in Python, we can use the collections module.

For instance, we write:

from collections import Counter

words = "apple banana apple strawberry banana lemon"
counts = Counter(words.split())
print(counts)

We use the Counter class with the words string split into a list with split by the spaces.

This lets us get the count of each word in a string.

Therefore, counts is Counter({'apple': 2, 'banana': 2, 'strawberry': 1, 'lemon': 1}).

Conclusion

To get the item frequency count in Python, we can use the collections module.

Categories
Python Answers

How to get the date from the week number with Python?

Sometimes, we want to get the date from the week number with Python.

In this article, we’ll look at how to get the date from the week number with Python.

How to get the date from the week number with Python?

To get the date from the week number with Python, we can use the datetime.datetime.strptime method.

For instance, we write:

import datetime

d = "2020-W26"
r = datetime.datetime.strptime(d + '-1', "%Y-W%W-%w")
print(r)

We call datetime.datetime.strptime with the d date string.

And then we convert the d string to a date by concatenating it with '-1' and use "%Y-W%W-%w" as the format string.

Finally, we assign the returned date to r.

Therefore, r is '2020-06-29 00:00:00'.

Conclusion

To get the date from the week number with Python, we can use the datetime.datetime.strptime method.

Categories
Python Answers

How to save a new sheet in an existing Excel file using Pandas in Python?

Sometimes, we want to save a new sheet in an existing Excel file using Pandas in Python.

In this article, we’ll look at how to save a new sheet in an existing Excel file using Pandas in Python.

How to save a new sheet in an existing Excel file using Pandas in Python?

To save a new sheet in an existing Excel file using Pandas in Python, we can use the pd.ExcelWriter class.

For instance, we write:

import pandas as pd
import numpy as np

path = "data.xlsx"

x1 = np.random.randn(100, 2)
df1 = pd.DataFrame(x1)

x2 = np.random.randn(100, 2)
df2 = pd.DataFrame(x2)

writer = pd.ExcelWriter(path, engine = 'xlsxwriter')
df1.to_excel(writer, sheet_name = 'x1')
df2.to_excel(writer, sheet_name = 'x2')
writer.save()
writer.close()

We create 2 data frames with pd.DataFrame.

Then we use the pd.ExcelWriter class with the path and engineto create thewriter` object to write the data to an Excel file.

We install Xlsxwriter by running:

pip install XlsxWriter

to use 'xlsxwriter' as the engine.

Next, we call “ on the data frames with writer and the sheet_name value to write the data frames into their own sheets.

Finally, we call writer.save to save the Excel file and writer.close to close the writer.

Now we should see the values in the data frames written into the spreadsheet file.

Conclusion

To save a new sheet in an existing Excel file using Pandas in Python, we can use the pd.ExcelWriter class.

Categories
Python Answers

How to find the common elements between 2 lists with Python?

Sometimes, we want to find the common elements between 2 lists with Python.

In this article, we’ll look at how to find the common elements between 2 lists with Python.

How to find the common elements between 2 lists with Python?

To find the common elements between 2 lists with Python, we can convert the first list to a set and use the set’s intersection method.

For instance, we write:

list1 = [1, 2, 3, 4, 5, 6]
list2 = [3, 5, 7, 9]
intersection = list(set(list1).intersection(list2))
print(intersection)

We have 2 lists list1 and list2 and we want to get the intersection between them.

To do this, we call set with list1 to convert it to a set.

Then we can call intersection on it with list2 to return a set that has values from both lists.

Finally, we call list to return a list by converting it from the intersection set.

Therefore, intersection is [3, 5].

Conclusion

To find the common elements between 2 lists with Python, we can convert the first list to a set and use the set’s intersection method.

Categories
Python Answers

How to rename a dictionary key with Python?

Sometimes, we want to rename a dictionary key with Python.

In this article, we’ll look at how to rename a dictionary key with Python.

How to rename a dictionary key with Python?

To rename a dictionary key with Python, we can use the dictionary’s pop method.

For instance, we write:

d = {0: 0, 1: 1, 2: 2, 3: 3}
k_old = 0
k_new = 4
d[k_new] = d.pop(k_old)
print(d)

We have the dictionary d, and we want to change the key of the first entry from 0 to 4.

To do this, we call d.pop with k_old to remove the the entry with key 0.

And then we set the entry with key k_new to the dictionary entry value returned by pop, which is the value 0.

Therefore, d is {1: 1, 2: 2, 3: 3, 4: 0} according to the print output.

Conclusion

To rename a dictionary key with Python, we can use the dictionary’s pop method.