Categories
Python Answers

How to get a union of strings with Python Pandas groupby?

Sometimes, we want to get a union of strings with Python Pandas groupby.

In this article, we’ll look at how to get a union of strings with Python Pandas groupby.

How to get a union of strings with Python Pandas groupby?

To get a union of strings with Python Pandas groupby, we can use groupby with apply.

For instance, we write:

import pandas as pd

df = pd.DataFrame({'A': [1, 1, 3], 'B': [4, 5, 6]})
s = df.groupby('A')['B'].apply(list)
print(s)

to create the df data frame with pd.DataFrame.

Then we call df.groupby with 'A' and 'B' to group the values in column 'B' by the values of column 'A'.

And then we call apply with list to put the grouped values into lists.

Therefore, s is:

A
1    [4, 5]
3       [6]
Name: B, dtype: object

Conclusion

To get a union of strings with Python Pandas groupby, we can use groupby with apply.

Categories
Python Answers

How to find a file with Python?

Sometimes, we want to find a file with Python.

In this article, we’ll look at how to find a file with Python.

How to find a file with Python?

To find a file with Python, we can use the os.walk method.

For instance, we write:

import os


def find(name, path):
    for root, dirs, files in os.walk(path):
        if name in files:
            return os.path.join(root, name)


print(find('data.xlsx', './'))

We defined the find function that calls os.walk with path.

In the function we loop through the files and directories returned from the iterator starting from the path‘s level and below.

And we check if the name is in the files array to check if the file with the given name is in the files array.

If that’s True, then we return the full path of the file with os.path.join.

Therefore, if the file is found, then we get something like './data.xlsx'. from print.

Conclusion

To find a file with Python, we can use the os.walk method.

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.