Categories
Python Answers

How to count frequency of values in Python Pandas DataFrame column?

Sometimes, we want to count frequency of values in Python Pandas DataFrame column.

In this article, we’ll look at how to count frequency of values in Python Pandas DataFrame column.

How to count frequency of values in Python Pandas DataFrame column?

To count frequency of values in Python Pandas DataFrame column., we can use the value_counts method.

For instance, we write

counts = df['status'].value_counts().to_dict()
print(counts)

to call value_counts on the df data frame’s status column to count all the items in the status column.

And then we call to_dict to return the item as the keys and the count of each item in status as the values.

Conclusion

To count frequency of values in Python Pandas DataFrame column., we can use the value_counts method.

Categories
Python Answers

How to do variable subtraction in Python Django templates?

Sometimes, we want to do variable subtraction in Python Django templates.

In this article, we’ll look at how to do variable subtraction in Python Django templates.

How to do variable subtraction in Python Django templates?

To do variable subtraction in Python Django templates, we can use the add filter.

For instance, we write

{{ myval|add:"-5" }}

to add -5 to myval by using the add filter with '-5' in the curly braces.

Conclusion

To do variable subtraction in Python Django templates, we can use the add filter.

Categories
Python Answers

How to get all the request headers in Python Django?

Sometimes, we want to get all the request headers in Python Django.

In this article, we’ll look at how to get all the request headers in Python Django.

How to get all the request headers in Python Django?

To get all the request headers in Python Django, we can use the request.headers property.

For instance, in our view, we write

request.headers['User-Agent']

to get the User-Agent request header.

request.headers is a dict.

Conclusion

To get all the request headers in Python Django, we can use the request.headers property.

Categories
Python Answers

How to pass query parameters via Python Django’s {% url %} template tag?

Sometimes, we want to pass query parameters via Python Django’s {% url %} template tag.

In this article, we’ll look at how to pass query parameters via Python Django’s {% url %} template tag.

How to pass query parameters via Python Django’s {% url %} template tag?

To pass query parameters via Python Django’s {% url %} template tag, we just append it after the URL.

For instance, in our template, we write

<a href="{% url 'myview' %}?office=foobar">
  ...
</a>

to add ?office=foobar after the URL for the myview view.

Conclusion

To pass query parameters via Python Django’s {% url %} template tag, we just append it after the URL.

Categories
Python Answers

How to only accept a certain file type in FileField with Python Django?

To only accept a certain file type in FileField with Python Django, we can add a validator function to check the file extension.

For instance, we write

def validate_file_extension(value):
    import os
    from django.core.exceptions import ValidationError
    ext = os.path.splitext(value.name)[1]  # [0] returns path+filename
    valid_extensions = ['.pdf', '.doc', '.docx', '.jpg', '.png', '.xlsx', '.xls']
    if not ext.lower() in valid_extensions:
        raise ValidationError('Unsupported file extension.')

class Document(models.Model):
    file = models.FileField(upload_to="documents/%Y/%m/%d", validators=[validate_file_extension])

to define the validate_file_extension function to check the file extension of the selected file’s path.

We get the extension from the path with os.path.splitext(value.name)[1].

And then we check if the ext extension is in valid_extensions.

If it’s not, then we raise a ValidationError.

Next, in our Document model, we add a FileField with the validators arguments set to an array with the validate_file_extension as the value.

Then validate_file_extension when a file is uploaded.