how to save Python pandas data into excel file? - python

I am trying to load data from the web source and save it as a Excel file but not sure how to do it. What should I do? The original dataframe has different columns. Let's say that I am trying to save 'Open' column
import matplotlib.pyplot as plt
import pandas_datareader.data as web
import datetime
import pandas as pd
def ViewStockTrend(compcode):
start = datetime.datetime(2015,2,2)
end = datetime.datetime(2016,7,13)
stock = web.DataReader(compcode,'yahoo',start,end)
print(stock['Open'])
compcode = ['FDX','GOOGL','FB']
aa= ViewStockTrend(compcode)

Once you have made the pandas dataframe just use to_excel on the entire thing if you want:
aa.to_excel('output/filename.xlsx')

If stock is a pandas DataFrame, you need to construct a new Framefrom that column and output that one to excel:
df = pd.DataFrame(stock['Open'])
df.to_excel('path/to/your/file')

Related

python pandas how to apply inplace = True for replace data and get updated csv

I want to keep change updated data and download the csv. how to do that in pandas here is my code:
data = pd.read_csv("product.csv")
data['image'] = data['image'].str.strip("[]'")
data["image"]=data["image"].str.split(",")
But I am getting old value when type data in my console. how to keep change updated value and download the csv?
Pandas does not modify CSV files in place.
Have you simply forgotten to save your DataFrame?
import pandas as pd
data = pd.read_csv("product.csv")
data['image'] = data['image'].str.strip("[]'").str.split(',')
data.to_csv('product.csv')

Using Pandas to pull excel document info and save selected columns to new file

Hey guys really confused as to how to approach this, tried looking all over the place. I want to save the selected columns in a new excel file. Any help is appreciated!
import pandas as pd
import numpy as np
data = pd.read_excel('C:\\Users\\me\\Downloads\\Reconcile.xlsx')
data[['batched_at', 'batch_id', 'total', 'customer_firstname', 'customer_lastname']]
data.to_excel('C:\\Users\\me\\Downloads\\Newfile.xlsx')
The third line does nothing here, assign it to a new dataframe and save that one.
import pandas as pd
import numpy as np
data = pd.read_excel('C:\\Users\\me\\Downloads\\Reconcile.xlsx')
new_data = data[['batched_at', 'batch_id', 'total', 'customer_firstname', 'customer_lastname']]
new_data.to_excel('C:\\Users\\me\\Downloads\\Newfile.xlsx')

How to Extract the result from python into a xls file

I'm a novice in python and I need to extract references from scientific literature. Following is the code I'm using
from refextract import extract_references_from_url
references = extract_references_from_url('https://arxiv.org/pdf/1503.07589.pdf')
print(references)
So, Please guide me on how to extract this printed information into a Xls file. Thank you so much.
You could use the pandas library to write the references into excel.
from refextract import extract_references_from_url
import pandas as pd
references = extract_references_from_url('https://arxiv.org/pdf/1503.07589.pdf')
print(references)
# convert to pandas dataframe
dfref = pd.DataFrame(references)
# write dataframe into excel
dfref.to_excel('./refs.xlsx')
You should have a look at xlsxwriter, a module for creating excel files.
Your code could then look like this:
import xlsxwriter
from refextract import extract_references_from_url
workbook = xlsxwriter.Workbook('References.xlsx')
worksheet = workbook.add_worksheet()
references = extract_references_from_url('https://arxiv.org/pdf/1503.07589.pdf')
row = 0
col = 0
worksheet.write(references)
workbook.close
(modified based upon https://xlsxwriter.readthedocs.io/tutorial01.html)
After going through the documentation of refextract here, I found that your variable references is a dictionary. For converting such a dictionary to python you can use Pandas as follows-
import pandas as pd
# create a pandas dataframe using a dictionary
df = pd.DataFrame(data=references, index=[0])
# Take transpose of the dataframe
df = (df.T)
# write the dictionary to an excel file
df.to_excel('extracted_references.xlsx')

Problem when importing Excel File with Pandas

I'm new to python and was hoping someone could help me out.
I imported an excel file using pandas just to play around with. However when I try do any additional analysis or coding on the data it is only using the header row of the excel file.
Here's one of the codes I used:
import pandas as pd
df = pd.read_excel(r'C:\Users\at0789\Documents\Test File.xlsx')
data=list(df)
print(data)
Here's the output:
runfile('C:/Users/at0789/.spyder-py3/temp.py', wdir='C:/Users/at0789/.spyder-py3')
['Name', 'Number', 'Color', 'Date']
This is what my test file looks like:
you can pass only the string 'C:\Users\at0789\Documents\Test File.xlsx'
And you don't have to print the df, only call it, like that
import pandas as pd
df = pd.read_excel('C:\Users\at0789\Documents\Test File.xlsx')
df
import pandas as pd
df = pd.read_excel(r'C:\Users\at0789\Documents\Test File.xlsx')
df - data-frame
Data-frame have some many built-in function. With optimisation code with less line of code and high performance
One best feature is play example play with data as like sql query

Save an object from pandas_datareader into a csv file in Python 3.5

In order to save stock prices from yahoo into Python 3.5, I use the pandas module :
from pandas_datareader import data as dreader
symbols = ['AAPL','MRK']
pnls = {i:dreader.DataReader(i,'yahoo','2010-01-01','2016-09-01') for i in symbols}
It creates two "tables" (I don't know the name, sorry), one for each share (here 'AAPL' and 'MRK'). I want to save each table into a csv file but I don't know how. Anyone does?
Thanks,
Anthony
Just do this:
from pandas_datareader import data as dreader
symbols = ['AAPL','MRK']
for i in symbols:
dreader.DataReader(i,'yahoo','2010-01-01','2016-09-01').to_csv(i+'.csv')
It saves your data to two csv files.
It actually returns a pandas DataFrame. You can easily put a pandas DataFrame to csv file using the to_csv method.

Categories