Friday, April 24, 2020

Pandas: Sheet Formating

Testing some Jupyter formating. pandas_format

1 - Default look of pandas sheet

note: style is default style
In [84]:
import pandas as pd
df = pd.read_csv("test.csv")
df.style
Out[84]:
name
0 Adam, Bobo
1 John, Doe
2 Mary, Johnsom
3 Zikie, Bath
4 Beth, Matties

2 - With background color

In [32]:
import pandas as pd
df = pd.read_csv("test.csv")
df.head().style.set_properties(**{'background-color': 'grey',
                           'color': 'yellow',
                           'border-color': 'red'})
Out[32]:
name
0 Adam, Bobo
1 John, Doe
2 Mary, Johnsom
3 Zikie, Bath
4 Beth, Matties

3 - With color axis and highlight

In [71]:
import pandas as pd
df = pd.read_csv("test.csv")

df.head().style.set_table_styles( 
[ 
 {'selector': 'th', 
  'props': [('background', 'yellow'),  
            ('color', 'black'), ]}, 
 {'selector': 'td', 
  'props': [('color', 'blue')]}, 
 { 'selector': 'tr:hover',
    'props': [('background-color', 'red')]}
])
Out[71]:
name
0 Adam, Bobo
1 John, Doe
2 Mary, Johnsom
3 Zikie, Bath
4 Beth, Matties

4 - Combining both set_table_styles with set_properties. Table border doesn't exist in set_table_styles

In [83]:
import pandas as pd
df = pd.read_csv("test.csv")

z = df.head().style.set_table_styles( 
[ 
 {'selector': 'th', 
  'props': [('background', 'yellow'),  
            ('color', 'black'), ]}, 
 {'selector': 'td', 
  'props': [('color', 'blue')]}, 
 { 'selector': 'tr:hover',
    'props': [('background-color', 'orange')]}
])
z.set_properties(**{'background-color': 'grey-light',
                    'border-style': 'solid'})
Out[83]:
name
0 Adam, Bobo
1 John, Doe
2 Mary, Johnsom
3 Zikie, Bath
4 Beth, Matties

5 - The following will overwrite entire notebook with thick border

In [75]:
%%HTML
<style type="text/css">
table.dataframe td, table.dataframe th {
    border-style: solid;
    background-color: gray-light;
}
</style>
In [40]:
import pandas as pd
df = pd.read_csv("test.csv")
df.head()
Out[40]:
name
0 Adam, Bobo
1 John, Doe
2 Mary, Johnsom
3 Zikie, Bath
4 Beth, Matties





References



Pandas: SQL Like pandas operations

Pandas's SQL Like operations such as WHERE clause. = != >= str.contains() & | .isin() .isnull() .notnull() ....