@norberto
To filter stocks in a large dataset using Python, you can follow these steps:
- Import the necessary libraries:
- Load your stock dataset into a pandas DataFrame:
1
|
df = pd.read_csv('stock_data.csv') # Make sure the dataset is in the same directory or provide the correct path
|
- Explore the data to understand its structure and available columns:
1
2
|
print(df.head()) # Display the first few rows to get an idea of the dataset
print(df.info()) # Get general information about the dataset, including column names and data types
|
- Apply filters to the DataFrame based on desired criteria. For example, if you want to filter stocks that have a price greater than $50 and a volume greater than 100,000, you can use the following code:
1
|
filtered_stocks = df[(df['Price'] > 50) & (df['Volume'] > 100000)]
|
- Optionally, you can refine your filter criteria by adding multiple conditions or using other logical operators. For instance, filtering stocks from a specific sector:
1
|
filtered_stocks = df[(df['Price'] > 50) & (df['Volume'] > 100000) & (df['Sector'] == 'Technology')]
|
- Finally, you can examine the filtered stocks by printing the resulting DataFrame:
Remember to adjust the column names and filter criteria based on your specific dataset.