Scanning Finviz Stocks - python

Hello here is a screener tool for Finviz but my stock_list result returns object of type Screener I am trying to put that into a data frame but I am having issues as the data is one long string divided by pipes. I tried to use str but method does not exists in the screener class. I am new to python this looks easy but I just dont know the proper syntax here can anyone help. Thank you!
import pandas as pd
import nest_asyncio
from finviz.screener import Screener
import csv
import sys
from datetime import datetime
nest_asyncio.apply()
filters = ['idx_sp500'] # Shows companies in NASDAQ which are in the S&P500
stock_list = Screener(filters=filters, order='price')

You could output the data to a csv file and read the file using pandas:
stock_list = Screener(filters=filters, order='price')
stock_list.to_csv(filename="stocks.csv")
df = pd.read_csv("stocks.csv")

Related

Why cant I extract a single column using pandas?

I have a (theoretically) simple task. I need to pull out a single column of 4000ish names from a table and use it in another table.
I'm trying to extract the column using pandas and I have no idea what is going wrong. It keeps flagging an error:
TypeError: string indices must be integers
import pandas as pd
file ="table.xlsx"
data = file['Locus tag']
print(data)
You have just add file name and define the path . But you cannot load the define pandas read excel function . First you have just the read excel function from pandas . That can be very helpful to you read the data and extract the column etc
Sample Code
import pandas as pd
import os
p = os.path.dirname(os.path.realpath("C:\Car_sales.xlsx"))
name = 'C:\Car_sales.xlsx'
path = os.path.join(p, name)
Z = pd.read_excel(path)
Z.head()
Sample Code
import pandas as pd
df = pd.read_excel("add the path")
df.head()

Is there a function to print JSON schema in Python?

I read in the JSON file as Pandas data frame. Now I want to print the JSON object schema. I look around and mostly I only saw links to do this online but my file is too big (almost 11k objects/lines). I'm new at this so I was wondering is there a function/code that I can do this in python?
What I have so far...
import json
import pandas as pd
df = pd.read_json('/Users/name/Downloads/file.json', lines = True)
print(df)
I can't add a comment, but, maybe if you try to convert the df into json inside a variable and then print the variable.
you can if you use the pydantic and datamodel-code-generator libraries.
Use datamodel-code-generator to produce the Python model and then use pydantic to print out the schema from the model.

Converting HTML table to CSV file using python

I am very new to pandas, so I wanted to convert this HTML table to CSV file with the pandas however my CSV file is giving me a weird sign and it didn't manage to covert all the table over to the CSV.
Here's my code. I read about using beautifulsoup but I'm not too sure how to use the function.
import as pandas
df = pd.read_html('https://aim-sg.caas.gov.sg/aip/2020-10-13/final/2020-09-10-Non-AIR'
'AC/html/eAIP/ENR-3.1-en-GB.html?s=B2EE1C5E1D2A684224A194E69D18338A560504FC#ENR-3.1')
df[0].to_csv('ENR3.0.csv')
Thank you!
Edited: I have changed my import to import pandas as dp but i still did not manage to convert all the HTML table to CSV file.
Greatly appreciate all your help!
You can use pandas itself to do this. You have messed up with the import statement. Here is how you do it correctly:
import pandas as pd
df = pd.read_html('https://aim-sg.caas.gov.sg/aip/2020-10-13/final/2020-09-10-Non-AIR'
'AC/html/eAIP/ENR-3.1-en-GB.html?s=B2EE1C5E1D2A684224A194E69D18338A560504FC#ENR-3.1')
df[0].to_csv('ENR3.0.csv', index = False)
If you want to get all the dataframes present within the variable df, then replace the last line with this:
for x in range(len(df)):
df[x].to_csv(f"CSV_File_{x+1}", index = False)
There is issue in import statement
It should be import pandas as pd and not import as pandas, as your are using alias pd in the code below.
Study about beautiful soup and use lxml parser to parse required data ( it is very fast ).
This link might help you out:
BeautifulSoup different parsers
If any other help is required, then do leave a comment on this post and will try to sort our your issue :)
Made correction in your code:
import pandas as pd
df = pd.read_html('https://aim-sg.caas.gov.sg/aip/2020-10-13/final/2020-09-10-Non-AIR'
'AC/html/eAIP/ENR-3.1-en-GB.html?s=B2EE1C5E1D2A684224A194E69D18338A560504FC#ENR-3.1')
df[0].to_csv('ENR3.0.csv')

Slicing a spreadsheet in Python terminal

So I'm making a program that asks a server a data and it send back. The problem is when I ask for some data it answer:
And I need just the value from the column "Close" . I can't figure out how to do it.
Code:
import yfinance as yf
import time
stock = yf.Ticker("MSFT")
data = stock.history(period="1")
oferta= stock.info["bid"]
print(data)
yfinance uses the pandas library to return data. You are receiving a pandas' DataFrame object in data, so to select only the "Close" column you can do this:
print(data["Close"].iloc[0])

pandas yahoo finance real time data

Is it possible to get real time stock data with pandas from yahoo finance?
For historical data i would do the following:
import pandas.io.data as web
aapl = web.get_data_yahoo('AAPL')
Is there a way to get the latest aapl price?
EDIT:
Yahoo has ended their free finance API so this answer is no longer relevant. Below is my answer for pre-2019 purposes.
Archival:
There's plenty of libraries available for this. Pandas doesn't explicitly do this though.
Most simply, I would suggest you just use a web library to download yahoo data. I like using requests, but you could also use urllib. You can coerce the response into a data frame after you get it.
import requests
requests.get("http://download.finance.yahoo.com/d/quotes.csv?s=AAPL&e=.csv&f=nsl1op").text
The nsl1op format var is from the docs:
I will use here the tags of name(n), symbol(s), the latest value(l1), open(o) and the close value of the last trading day(p)
Your response data should look something like
u'"Apple Inc.","AAPL",114.63,113.50,115.07\r\n'
You can just split up the string using the csv library and throw it into a data frame from there
To answer your question about using Pandas specifically, you can pull stock data from yahoo using pandas like so:
from pandas.io.data import DataReader
from datetime import datetime
aapl = DataReader('AAPL', 'yahoo', datetime(2015,7,1), datetime(2015,7,1))
print(aapl['Adj Close'][0])
This code results in:
126.599998
The other keys you can use are Open, Close, High, Low, and Volume.
Keep in mind that the returned dataset is an array. You need to enumerate said array to get the data, either by specifying your index, or with a for loop.
You can use below code to get all info from yahoo finace api:
import pandas as pd
from pandas_datareader import data as wb
aapl=wb.DataReader('AAPL',start='2015-1-1',data_source='yahoo')
print(aapl)

Categories

Resources