Using Colab, 'to_excel' method directory is wrong? - google-colaboratory

from pandas import Series
data = [1000, 2000, 3000]
index = ['a', 'b', 'c']
s = Series(data = data, index = index)
s.to_excel('/content/drive/MyDrive/Colab Notebooks/data.xlsx')
This does not work and it says
[Errno 2] No such file or directory:
But I do have such directories and same file names!!

Related

How to change pandas display font of index column

data = {
'X': [3, 2, 0, 1],
'Y': [0, 3, 7, 2]
}
df = pd.DataFrame(data, index=['A', 'B', 'C', 'D'])
df.style.set_properties(**{
'font-family':'Courier New'
})
df
The index column is displayed in bold, is it possible to change font of index column?
You must use table_styles. In this example I manage to make the "font-weight":"normal" for the index and columns:
Let's define some test data:
import pandas as pd
df = pd.DataFrame({'A':[1,2,3,4],
'B':[5,4,3,1]})
We define style customization to use:
styles = [
dict(selector="th", props=[("font-weight","normal"),
("text-align", "center")])]
We pass the style variable as the argument for set_table_styles():
html = (df.style.set_table_styles(styles))
html
And the output is:
Please feel free to read about the documentation in pandas Styling for more details.

Applying style to index labels and column labels pandas dataframe

I have a dataframe which when dump to excel appears as following:
I need to dump to excel with formatting such that it appears as:
i.e. I have a dictionary which is used to apply color to column name and index names.
colorIndex = {'A':'Bb', 'B':'B'}
colorColumn = {'ATC':'X1', 'P25':'Y'}
I am using the following code to generate dataframe and dump to excel:
import pandas as pd, numpy as np, sys, os
def getDF():
df = pd.DataFrame()
df['ATC'] =np.random.rand(1, 7).round(2).flatten()
df['P25'] =np.random.rand(1, 7).round(2).flatten()
df['P75'] =np.random.rand(1, 7).round(2).flatten()
df['Type1'] = ['A', 'B', 'B', 'A', 'B', 'B', 'A']
df['Type11'] = ['A', 'Aa', 'Bb', 'A', 'Bb', 'B', 'Bb']
df['Type2'] = ['X', 'X', 'X1', 'Y', 'Y', 'Y1', 'Y']
df = df.pivot_table(index=['Type1', 'Type11'], columns='Type2', aggfunc=[np.mean])['mean']
return df
df = getDF()
fn = r'C:\Users\Desktop\format_file.xlsx'
df.to_excel(fn, engine='openpyxl')
But I don't have clue how to generate the style parameters for this kind of excel dump.

Cannot save as csv using QFileDialog.getSaveFileName

df = pd.DataFrame({'col1': [1, 2], 'col2': [3, 4]})
fileName = QFileDialog.getSaveFileName(self,"Save",os.getcwd(),"CSV Files (*.csv)")
if fileName:
with open(fileName, "w") as file:
file.write(df)
I am tring to save my dataframe to csv using QFileDialog instead of df.to_csv, but this doesn't work
fileName = QFileDialog.getSaveFileName(self,"Save",os.getcwd(),"CSV Files (*.csv)")
print(fileName)
this returns strings in tuple, first is path
you should write:
fileName, _ = QFileDialog.getSaveFileName(self,"Save",os.getcwd(),"CSV Files (*.csv)")

Invalid Syntax error pandas series

I am starting out with pandas on jupyter notebook. In the error message, there is a ^ below the = operator, but I cannot see the problem. What's missing? thanks!
import pandas as pd
data2 = ([1, 2, 3, 4], index = ['a', 'b', 'c', 'd'])
s = pd.Series(data2)
print(s.shape)
This is the error:
File "<ipython-input-30-57c99bd7e494>", line 4
data2 = ([1, 2, 3, 4], index = ['a', 'b', 'c', 'd'])
^
SyntaxError: invalid syntax
There proper way to do this is, separate variables for data and index:
import pandas as pd
data2 = [1,2,3,4]
index = ['a','b','c','d']
s = pd.Series(data2,index)
print(s.shape)
Or as ayhan points our you could unpack a dictionary with **:
data2 = dict(data=[1,2,3,4], index=['a','b','c','d'])
s = pd.Series(**data2)
print(s.shape)

How to skip key error in pandas?

I have a dictionary and a list. For each key in the list, I want to plot the associated values with that key.
I have the following code in pandas:
import numpy as np; np.random.seed(22)
import seaborn as sns; sns.set(color_codes=True)
window = int(math.ceil(5000.0 / 100))
xticks = range(-2500,2500,window)
sns.tsplot([mydictionary[k] for k in mylist],time=xticks,color="g")
plt.legend(['blue'])
However, I get KeyError: xxxx
I can manually remove all problematic keys in my list, but that will take a long time. Is there a way I can skip this key error?
If you are looking for a way to just swallow the key error, use a try & except. However, cleaning up the data in advance would be much more elegant.
Example:
mydictionary = {
'a': 1,
'b': 2,
'c': 3,
}
mylist = ['a', 'b', 'c', 'd']
result = []
for k in mylist:
try:
result.append(mydictionary[k])
except KeyError:
pass
print(result)
>>> [1, 2, 3]
You will need to construct the list prior to using it in your seaborn plot. Afterwards, pass the list with the call:
sns.tsplot(result ,time=xticks,color="g")