I made an excel sheet using pandas dataframe to generate texts with clickable urls using the following code
import pandas as pd
df = pd.DataFrame({'link':['=HYPERLINK("https://ar.wikipedia.org/wiki/","wikipidia")',
'=HYPERLINK("https://www.google.com", "google")']})
df.to_excel('links.xlsx')
But currently i need to read the generated excel sheet (links.xlsx) using pandas.read_excel so i tried the following code:
import pandas as pd
excelDf=pd.read_excel('links.xlsx')
print(excelDf)
but this generates a dataframe with all zeroes in the link column.
Is there another way I can read the excel file i created, or another way to create an excel sheet containing clickable links on text using pandas dataframe that is readable?
you can do the same as a csv which is cleaner (avoids excel issues).
# %% write the date
import pandas as pd
df = pd.DataFrame({'link':['=HYPERLINK("https://ar.wikipedia.org/wiki/","wikipidia")',
'=HYPERLINK("https://www.google.com", "google")']})
df.to_csv('F:\\links.xlsx')
# %% read the data
import pandas as pd
excelDf=pd.read_csv('F:\\links.xlsx')
print(excelDf)
result:
Unnamed: 0 link
0 0 =HYPERLINK("https://ar.wikipedia.org/wiki/","w...
1 1 =HYPERLINK("https://www.google.com", "google")
Related
I'm working with some xlsx files and need to import them into program. I've written a script that handles everything I need it to do.
However, I need to apply filters to the table in excel before importing them in.
When I apply filters and prep the table to import into python, python reads the entire table ignoring all the filters.
My work around has been filtering what I need then copying that to a new sheet. Then when reading into excel I specify the new sheet of filtered data that I'm looking for.
Is there a way to read the filtered table into excel directly?
Or Should I just import the entire table and apply those same filters using pandas in my script instead?
IIUC, you can't read only visible rows and/or columns of an Excel spreadsheet with pandas.
To do that, you need some help from openpyxl (!pip install openpyxl) :
from openpyxl import openpyxl
import pandas as pd
wb = load_workbook("file.xlsx")
ws = wb.active # or wb["SheetName"] # <- change the name here
rows = [[c.value for c in r
if c.value and not ws.row_dimensions[r[0].row].hidden]
for r in ws.iter_rows()]
df = pd.DataFrame(data= rows[1:], columns=rows[0]).dropna()
Output :
print(df)
col col2
0 foo 1.0
2 baz 3.0
Input used (spreadsheet) :
For the past few days I've been trying to do a relatively simple task but I'd always encounter some errors so I'd really appreciate some help on this. Here goes:
I have an Excel file which contains a specific column (Column F) that has a list of IDs.
What I want to do is for the program to read this excel file and allow the user to input any of the IDs they would like.
When the user types in one of the IDs, I would want the program to return a bunch IDs that contain the text that the user has inputted, and after that I'd like to export those 'bunch of IDs' to a new & separate Excel file where all the IDs would be displayed in one column but in separate rows.
Here's my code so far, I've tried using arrays and stuff but nothing seems to be working for me :/
import pandas as pd
import numpy as np
import re
import xlrd
import os.path
import xlsxwriter
import openpyxl as xl;
from pandas import ExcelWriter
from openpyxl import load_workbook
# LOAD EXCEL TO DATAFRAME
xls = pd.ExcelFile('N:/TEST/TEST UTILIZATION/IA 2020/Dev/SCS-FT-IE-Report.xlsm')
df = pd.read_excel(xls, 'FT')
# GET USER INPUT (USE AD1852 AS EXAMPLE)
value = input("Enter a Part ID:\n")
print(f'You entered {value}\n\n')
i = 0
x = df.loc[i, "MFG Device"]
df2 = np.array(['', 'MFG Device', 'Loadboard Group','Socket Group', 'ChangeKit Group'])
for i in range(17367):
# x = df.loc[i, "MFG Device"]
if value in x:
df = np.array[x]
df2.append(df)
i += 1
print(df2)
# create excel writer object
writer = pd.ExcelWriter('N:/TEST/TEST UTILIZATION/IA 2020/Dev/output.xlsx')
# write dataframe to excel
df2.to_excel(writer)
# save the excel
writer.save()
print('DataFrame is written successfully to Excel File.')
Any help would be appreciated, thanks in advance! :)
It looks like you're doing much more than you need to do. Rather than monkeying around with xlsxwriter, pandas.DataFrame.to_excel is your friend.
Just do
df2.to_excel("output.xlsx")
You don't need xlsxwriter. Simply df.to_excel() would work. In your code df2 is a numpy array/ First convert it into a pandas DataFrame format a/c to the requirement (index and columns) before writing it to excel.
When I'm reading a csv file using the pandas library, but when I print the head of the dataframe it doesn't show it as a dataframe but more like a list.
This is the code I tipped:
import pandas as pd
df = pd.read_csv('Path/File')
print(df.head())
The output looks like this
How do I get it show the data frame properly?
I am new to pandas and using it to convert and excel sheet with formulas to csv.
As expected, I want to just copy the values. However, in my csv I am getting the header but all other cells have "0" reported.
import pandas as pd
data_xls = pd.read_excel('my_result.xlsx', 'Dashboard', index_col=None)
data_xls.to_csv('myabc_result.csv', encoding='utf-8',index=False)
The formula in original excel sheet looks like this(surprisingly every cell has similar formula):
=INDEX(INDIRECT("Results!"&MATCH(INDIRECT(SUBSTITUTE(ADDRESS(1,COLUMN(),4),"1","")&"$1"),Results!$A:$A,0)&":"&MATCH(INDIRECT(SUBSTITUTE(ADDRESS(1,COLUMN(),4),"1","")&"$1"),Results!$A:$A,0)),1,INDIRECT("$A"&ROW())+1)
Thanks,
Total beginner to python: Trying to import excel values from a column. Lookup the imported values in python dictionary (was able to create this) and then write the results into the excel file and see if they match to another column in the file.
You can use a module called pandas.
pip install pandas
To read the file use the following:
import pandas as pd
file = pd.ExcelFile('path/to/excelsheet/').parse('sheet_you_want_to_use') # 'Sheet 1' for Sheet 1
you can now access the columns using the column names as keys: file['column_name'].
You can now append the looked up values to a list. Then write to a excel file as follows:
list = ['....values....']
pd.DataFrame(list).to_excel('where/to/save/file')
I would advise you to read the following documentation:
pandas DataFrame
pandas ExcelFile
pandas to_excel
pandas