importing csv into jupyter with pandas - python

import pandas as pd
data=pd.read_csv(r"C:\Users\omananyi.yakubu\Desktop\pandas_test\RE07.01_OperatorActionsDetailed.csv")data.head()
I tried importing a CSV file but it came out with "invalid syntax".
Can someone help please?

Remove data from syntax:
import pandas as pd
data=pd.read_csv(r"C:\Users\omananyi.yakubu\Desktop\pandas_test\RE07.01_OperatorActionsDetailed.csv").head()

# Load the Pandas libraries with alias 'pd'
import pandas as pd
# Read data from file 'RE07.01_OperatorActionsDetailed.csv'
data = pd.read_csv("C:\Users\omananyi.yakubu\Desktop\pandas_test\RE07.01_OperatorActionsDetailed.csv")
#Preview the first 5 lines of the loaded data
data.head()

Related

How to load sframe format file in pandas?

Is there any way to directly open .sframe extension file in pandas.
Like an easy way
df = pd.read_csv('people.sframe')
Thank you.
No, you can't import sframe files directly with Pandas. Rather you can use a free python library named sframe:
import sframe
import pandas as pd
sf = sframe.SFrame('people.sframe')
Then you can convert it to a pandas DataFrame using:
df = sf.to_dataframe()

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')

Export a function to a .csv or .xlsx file in Python

I am using an apripori algorithm in Python and would like to know how can I export the results to excel or csv?
from mlxtend.preprocessing import TransactionEncoder
from mlxtend.frequent_patterns import apriori
apriori(df_one_hot,min_support=0.05,use_colnames=True).sort_values(by='support',ascending=False)
Store the results in a pandas dataframe and use to_csv to write to a CSV file.
import pandas as pd
results = apriori(df_one_hot,min_support=0.05,use_colnames=True).sort_values(by='support',ascending=False)
results_df = pd.DataFrame(results)
results_df.to_csv('results.csv', index=False)

error No module named 'xlrd'. how to import excel with python and pandas properly? please close this

I realized that there may be something wrong in my local dev env just now.
I tried my code on colab.
it worked well.
import pandas as pd
df = pd.read_excel('hurun-2018-top50.xlsx')
thank u all.
please close this session.
------- following is original description ---------
I am trying to import excel with python and pandas.
I already pip installed "xlrd" module.
I googled a lot and tried several different methods, none of them worked.
Here is my code.
import pandas as pd
from pandas import ExcelFile
from pandas import ExcelWriter
df = pd.read_excel('hurun-2018-top50.xlsx', index_col=0)
df = pd.read_excel('hurun-2018-top50.xlsx', sheetname='Sheet1')
df = pd.read_excel('hurun-2018-top50.xlsx')
Any response will be appreciated.

how to save Python pandas data into excel file?

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')

Categories