Categories
Python Answers

How to select rows in a DataFrame between two values, in Python Pandas?

Sometimes, we want to select rows in a DataFrame between two values, in Python Pandas.

In this article, we’ll look at how to select rows in a DataFrame between two values, in Python Pandas.

How to select rows in a DataFrame between two values, in Python Pandas?

To select rows in a DataFrame between two values, in Python Pandas, we can use the between method.

For instance, we write

df = df[df['closing_price'].between(99, 101)]

to select closing_price column values between 99 and 101 with

df[df['closing_price'].between(99, 101)]

Conclusion

To select rows in a DataFrame between two values, in Python Pandas, we can use the between method.

Categories
Python Answers

How to use Python Pandas read_csv with a URL?

Sometimes, we want to use Python Pandas read_csv with a URL.

In this article, we’ll look at how to use Python Pandas read_csv with a URL.

How to use Python Pandas read_csv with a URL?

To use Python Pandas read_csv with a URL, we just call read_csv with the URL of the CSV.

For instance, we write

import pandas as pd

url="https://raw.githubusercontent.com/cs109/2014_data/master/countries.csv"
c=pd.read_csv(url)

to call read_csv with the url of the CSV and assign the returned data frame to c.

Conclusion

To use Python Pandas read_csv with a URL, we just call read_csv with the URL of the CSV.

Categories
Python Answers

How to keep only date part when using Python pandas.to_datetime?

Sometimes, we want to keep only date part when using Python pandas.to_datetime.

In this article, we’ll look at how to keep only date part when using Python pandas.to_datetime.

How to keep only date part when using Python pandas.to_datetime?

To keep only date part when using Python pandas.to_datetime, we can use the dt.date method.

For instance, we write

df['just_date'] = df['dates'].dt.date

to convert the values in the dates columns to dates and put them into the just_date column.

We can also keep the dates values as the datetime64 data type with normalize:

df['normalised_date'] = df['dates'].dt.normalize()

Conclusion

To keep only date part when using Python pandas.to_datetime, we can use the dt.date method.

Categories
Python Answers

How to reset index in a Python Pandas dataframe?

Sometimes, we want to reset index in a Python Pandas dataframe.

In this article, we’ll look at how to reset index in a Python Pandas dataframe.

How to reset index in a Python Pandas dataframe?

To reset index in a Python Pandas dataframe, we can call reset_index on the data frame.

For instance, we write

df = df.reset_index(drop=True)

to call reset_index to return a new data frame with the indexes reset.

Conclusion

To reset index in a Python Pandas dataframe, we can call reset_index on the data frame.

Categories
Python Answers

How to do long to wide reshape, by two variables with Python Pandas?

Sometimes, we want to do long to wide reshape, by two variables with Python Pandas.

In this article, we’ll look at how to do long to wide reshape, by two variables with Python Pandas.

How to do long to wide reshape, by two variables with Python Pandas?

To do long to wide reshape, by two variables with Python Pandas, we can use groupby with cumcount.

For instance, we write

df['idx'] = df.groupby('Salesman').cumcount()

to call groupby to group values by the Salesman column.

And then call cumcount to return the cumulative counts of the grouped values.

Conclusion

To do long to wide reshape, by two variables with Python Pandas, we can use groupby with cumcount.