many pandas dataframe to in one readeable file - python-3.x

a=np.array([[1,1,1,1],[2,2,2,2],[3,3,3,3]])
df=pandas.DataFrame(a)
df.to_html('table.html')
I am trying to make pandas.DataFrame to html.
The data are from csv.file, and i split it to several table.
i want make table are in one file one by one.(it's not linked together)
open(filename, mode):
it likes file open with mode 'a',no overwrite original file.
Sample output table like this.
How can i do that or are there another more readable way ?

You can proceed as follows:
put all your dataframes in a list
iterate through them and convert each one to HTML
concatenate all the HTML outputs to one single file
In terms of code, here's how you can do that:
a=np.array([[1,1,1,1],[2,2,2,2],[3,3,3,3]])
b=np.array([[1,4,5,1],[12,2,32,2],[3,32,30,3]])
df_a = pd.DataFrame(a)
df_b = pd.DataFrame(b)
list_df = [df_a, df_b]
output = ""
for index, df in enumerate(list_df):
output += df.to_html() + '<br>'
with open('output.html', 'w') as f:
f.writelines(output)
Here's how the ouput looks like:
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>0</th>
<th>1</th>
<th>2</th>
<th>3</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<th>1</th>
<td>2</td>
<td>2</td>
<td>2</td>
<td>2</td>
</tr>
<tr>
<th>2</th>
<td>3</td>
<td>3</td>
<td>3</td>
<td>3</td>
</tr>
</tbody>
</table><br><table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>0</th>
<th>1</th>
<th>2</th>
<th>3</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>1</td>
<td>4</td>
<td>5</td>
<td>1</td>
</tr>
<tr>
<th>1</th>
<td>12</td>
<td>2</td>
<td>32</td>
<td>2</td>
</tr>
<tr>
<th>2</th>
<td>3</td>
<td>32</td>
<td>30</td>
<td>3</td>
</tr>
</tbody>
</table><br>

Related

Need the full correct html code for this Table

Image table
Please write full html codes for this table. You may see the Table image from above via the link.
<table border="1" width="800">
<tr>
<th>Level1</th>
<th>Level2</th>
<th>Level2</th>
<th>Info</th>
<th>Name</th>
</tr>
<tr>
<td rowspan="6">System</td>
</tr>
<tr>
<td rowspan="4">System Apps</td>
<td rowspan="2">System Memory</td>
</tr>
<tr>
<td rowspan="3">SystemEnv</td>
<td rowspan="1">SystemEnv2</td>
<td rowspan="2">Memeory Test</td>
</tr>
Here is the table code :
<table border="2">
<tr>
<th>Level 1</th>
<th>Level 2</th>
<th>Level 3</th>
<th>info</th>
<th>Name</th>
</tr>
<tr>
<td rowSpan="6">System</td>
<td rowSpan="4">System apps</td>
<td rowSpan="3">SystemEnv</td>
<td>App Text</td>
<td>foo</td>
</tr>
<tr>
<td>App memory</td>
<td>foo</td>
</tr>
<tr>
<td>App test</td>
<td>bar</td>
</tr>
<tr>
<td>Systemenv2</td>
<td>App test</td>
<td>bar</td>
</tr>
<tr>
<td rowSpan="2">System Memory</td>
<td rowSpan="2">Memory test</td>
<td>memory func</td>
<td>foo</td>
</tr>
<tr>
<td>Memory Func</td>
<td>foo</td>
</tr>
</table>

Creating a custom button in Jodit Editor

I'm trying to create a custom table button using Jodit React Editor using this as reference - https://xdsoft.net/jodit/examples/toolbar/custom_button.html
I'm a little lost on this though.
I need to be able to create a table and have the icon be a text icon that says - "Table"
Right now I've added this to my configurations - extraButtons: ['tableNew'].
I've also added the below code to the render method.
this.jodit.options.controls.tableNew = {
iconURL: '',
exec: function (editor) {
return '<table> <thead> <tr> <th> Sl no </th> <th>Name</th> <th>Age</th> </tr> </thead>'+
' <tbody> <tr> <td>1</td> <td></td> <td></td> </tr> </tbody> </table>';
}
};
I see that a space has been added in the toolbar which on hover says tableNew but on clicking it nothing happens.
I'd really appreciate it if someone could help me out with this.
Instead of the following,
return '<table> <thead> <tr> <th> Sl no </th> <th>Name</th> <th>Age</th> </tr> </thead> <tbody> <tr> <td>1</td> <td></td> <td></td> </tr> </tbody> </table>';
try something like this:
return editor.create.fromHTML('<table> <thead> <tr> <th> Sl no </th> <th>Name</th> <th>Age</th> </tr> </thead> <tbody> <tr> <td>1</td> <td></td> <td></td> </tr> </tbody> </table>');
This works for me (a similiar implementation, not returning table like you, but return some list).

MorningStar KeyStat to pandas Dataframe

I am trying to read keyStat in MorningStar and know the data which is HTML where is warped in a JSON. So far I can put a request that can get the json by Beautifulsoup:
url = 'http://financials.morningstar.com/ajax/keystatsAjax.html?t=tou&culture=en-CA&region=CAN'
lm_json = requests.get(url).json()
ksContent = BeautifulSoup(lm_json["ksContent"],"html.parser")
Now here is a bit wired to me that the html data as 'ksContent' which contains actual data as a table. I am not a fan of html and wondering how can I just make all it to a nice pandas dataframe? As the table is long, here is some of it:
<table cellpadding="0" cellspacing="0" class="r_table1 text2">
<colgroup>
<col width="23%"/>
<col span="11" width="7%"/>
</colgroup>
<thead>
<tr>
<th align="left" scope="row"></th>
<th align="right" id="Y0" scope="col">2008-12</th>
<th align="right" id="Y1" scope="col">2009-12</th>
<th align="right" id="Y2" scope="col">2010-12</th>
<th align="right" id="Y3" scope="col">2011-12</th>
<th align="right" id="Y4" scope="col">2012-12</th>
<th align="right" id="Y5" scope="col">2013-12</th>
<th align="right" id="Y6" scope="col">2014-12</th>
<th align="right" id="Y7" scope="col">2015-12</th>
<th align="right" id="Y8" scope="col">2016-12</th>
<th align="right" id="Y9" scope="col">2017-12</th>
<th align="right" id="Y10" scope="col">TTM</th>
</tr>
</thead>
<tbody>
<tr class="hr">
<td colspan="12"></td>
</tr>
<tr>
<th class="row_lbl" id="i0" scope="row">Revenue <span>CAD Mil</span></th>
<td align="right" headers="Y0 i0">—</td>
<td align="right" headers="Y1 i0">40</td>
<td align="right" headers="Y2 i0">212</td>
<td align="right" headers="Y3 i0">349</td>
<td align="right" headers="Y4 i0">442</td>
<td align="right" headers="Y5 i0">759</td>
<td align="right" headers="Y6 i0">1,379</td>
<td align="right" headers="Y7 i0">1,074</td>
<td align="right" headers="Y8 i0">1,125</td>
<td align="right" headers="Y9 i0">1,662</td>
<td align="right" headers="Y10 i0">1,760</td>
</tr> ...
It defines a header tr, Y0, Y1 ... Y10 as actual date and next tr refers to it.
your help appreciated!
You can use read_html() to convert it into a list of dataframes
import requests
import pandas as pd
url = 'http://financials.morningstar.com/ajax/keystatsAjax.html?t=tou&culture=en-CA&region=CAN'
lm_json = requests.get(url).json()
df_list=pd.read_html(lm_json["ksContent"])
You can iterate through it and get the dataframes one by one. You can also use dropna() to get rid of the NaN only rows.
Sample output screenshot from my jupyter Notebook

Convert linked images to embedded images

i have an excel file with many images inside.
Actually, this excel files was made with HTML code that converted to excel file and the images were linked to their respective image. ( The reason i made the file with excel is because there is about 700+ images and i cant make it manually from 1. It will takes so much time to finish)
here is my excel's HTML code
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<table>
<thead>
<tr>
<th>No</th>
<th>Image</th>
<th>Title</th>
</tr>
</thead>
<tbody>
<tr>
<td >1</td>
<td><img src="56.jpg" width="200px"/></td>
<td></td>
</tr>
<tr>
<td >2</td>
<td><img src="57.jpg" width="200px"/></td>
<td></td>
</tr>
<tr>
<td >3</td>
<td><img src="58.jpg" width="200px"/></td>
<td></td>
</tr>
<tr>
<td >4</td>
<td><img src="59.jpg" width="200px"/></td>
<td></td>
</tr>
<tr>
<td >5</td>
<td><img src="60.jpg" width="200px"/></td>
<td></td>
</tr>
<tr>
<td >6</td>
<td><img src="61.jpg" width="200px"/></td>
<td></td>
</tr>
<tr>
<td >7</td>
<td><img src="62.jpg" width="200px"/></td>
<td></td>
</tr>
<tr>
<td >8</td>
<td><img src="63.jpg" width="200px"/></td>
<td></td>
</tr>
<tr>
<td >9</td>
<td><img src="64.jpg" width="200px"/></td>
<td></td>
</tr>
<tr>
<td >10</td>
<td><img src="65.jpg" width="200px"/></td>
<td></td>
</tr>
<tr>
<td >11</td>
<td><img src="66.jpg" width="200px"/></td>
<td></td>
</tr>
<tr>
<td >12</td>
<td><img src="67.jpg" width="200px"/></td>
<td></td>
</tr>
<tr>
<td >13</td>
<td><img src="68.jpg" width="200px"/></td>
<td></td>
</tr>
<tr>
<td >14</td>
<td><img src="69.jpg" width="200px"/></td>
<td></td>
</tr>
<tr>
<td >15</td>
<td><img src="70.jpg" width="200px"/></td>
<td></td>
</tr>
<tr>
<td >16</td>
<td><img src="71.jpg" width="200px"/></td>
<td></td>
</tr>
<tr>
<td >17</td>
<td><img src="72.jpg" width="200px"/></td>
<td></td>
</tr>
<tr>
<td >18</td>
<td><img src="73.jpg" width="200px"/></td>
<td></td>
</tr>
<tr>
<td >19</td>
<td><img src="74.jpg" width="200px"/></td>
<td></td>
</tr>
<tr>
<td >20</td>
<td><img src="75.jpg" width="200px"/></td>
<td></td>
</tr>
<tr>
<td >21</td>
<td><img src="76.jpg" width="200px"/></td>
<td></td>
</tr>
<tr>
<td >22</td>
<td><img src="77.jpg" width="200px"/></td>
<td></td>
</tr>
<tr>
<td >23</td>
<td><img src="78.jpg" width="200px"/></td>
<td></td>
</tr>
<tr>
<td >24</td>
<td><img src="79.jpg" width="200px"/></td>
<td></td>
</tr>
</tbody>
</table>
</body>
</html>
The problem is, when i save it and send the excel file to other's PC, the images will be blank. Here's the screenshot
i tried to google the problem but still i couldn't find the solution yet.
Any method to convert all the images to embedded images?
Thank you
I wrote a VBA script to convert all linked images to embedded images.
This script will have errors if there are shapes which aren't images. It also assumes that embedded images have their link in their alt text, which I found to be the case in my situation.
Sub INSERT_PICS()
Dim shp As Shape
For Each shp In ActiveSheet.Shapes
Debug.Print shp.Name
Debug.Print shp.AlternativeText
ActiveSheet.Shapes.AddPicture Filename:=shp.AlternativeText, linktofile:=msoFalse, _
savewithdocument:=msoCTrue, Left:=shp.Left, Top:=shp.Top, Width:=shp.Width, Height:=shp.Height
shp.Delete
Next shp
End Sub

Search for a value and move it to another spreadsheet

I have a little problem using Excel 2010. The problem is simple but I think I start in a wrong direction. Let's say I have a document with the following structure:
<table>
<thead>
<th> Col A</th>
<th> Col B</th>
<th> Col C</th>
</thead>
<tr>
<td> NONE</td>
<td> TEST</td>
<td> NONE</td>
<td> TEST2</td>
<td> TEST3</td>
<td> TEST4</td>
</tr>
<tr>
<td> NONE</td>
<td> TEST</td>
<td> NONE</td>
<td> TEST2</td>
<td> TEST3</td>
<td> TEST4</td>
</tr>
<tr>
<td> TEST</td>
<td> TEST</td>
<td> NONE</td>
<td> TEST2</td>
<td> TEST3</td>
<td> TEST4</td>
</tr>
<tbody>
</tbody>
</table>
I want to search for NONE and move it to another spreadsheet. I want to do this with formula. I tried with SEACH but I don't know how to move it to another spreadsheet.

Resources