python pandas how to select range of data 1

python pandas how to select range of data

import numpy as np
import pandas as pd

df = pd.DataFrame(np.random.random((200,3)))
df['date'] = pd.date_range('2000-1-1', periods=200, freq='D')
mask = (df['date'] > '2000-6-1') & (df['date'] <= '2000-6-10')
print(df.loc[mask])

Here is what the above code is Doing:
1. Create a DataFrame of random numbers with a date index.
2. Create a mask that is True for the dates in June.
3. Use the mask to filter the DataFrame.

Similar Posts