Categories
Python Answers

How to save a Python Pandas DataFrame table as a png

To save a Python Pandas DataFrame table as a png, we an use the savefig method.

For instance, we write

import matplotlib.pyplot as plt
import pandas as pd
from pandas.table.plotting import tablebelow

ax = plt.subplot(111, frame_on=False)
ax.xaxis.set_visible(False)  
ax.yaxis.set_visible(False)  

table(ax, df)

plt.savefig('mytable.png')

to call subplot to create a subplot.

We hide the frame with

ax = plt.subplot(111, frame_on=False)

We hide the x and y axes with

ax.xaxis.set_visible(False)  
ax.yaxis.set_visible(False)  

And we fill the data and plot them with

table(ax, df)

Then we save the plot with

plt.savefig('mytable.png')
Categories
Python Answers

How to create dataframe from a dictionary where entries have different lengths with Python Pandas?

Sometimes, we want to create dataframe from a dictionary where entries have different lengths with Python Pandas.

In this article, we’ll look at how to create dataframe from a dictionary where entries have different lengths with Python Pandas.

How to create dataframe from a dictionary where entries have different lengths with Python Pandas?

To create dataframe from a dictionary where entries have different lengths with Python Pandas, we can use dictionary comprehension.

For instance, we write

import pandas as pd
import numpy as np

d = dict( A = np.array([1,2]), B = np.array([1,2,3,4]) )
    
pd.DataFrame(dict([ (k,pd.Series(v)) for k,v in d.items() ]))

to create the dictionary d that has the some values in it.

And then we use (k,pd.Series(v)) for k,v in d.items() to return a dictionary with the same name on each column.

And then we use the returned dictionary with the DataFrame class to create the data frame.

The empty values are filled with NaN.

Conclusion

To create dataframe from a dictionary where entries have different lengths with Python Pandas, we can use dictionary comprehension.

Categories
Python Answers

How to filter rows of DataFrame with operator chaining with Python Pandas?

To filter rows of DataFrame with operator chaining with Python Pandas, we can use the data frame query method.

For instance, we write

df_filtered = df.query('a > 0 and 0 < b < 2')

to call df.query with the conditions we want for the values to return a new data frame with the filtered values.

Categories
Python Answers

How to convert Python Pandas timezone-aware DateTimeIndex to naive timestamp, but in certain timezone?

To convert Python Pandas timezone-aware DateTimeIndex to naive timestamp, but in certain timezone, we call tz_localize.

For instance, we write

pd.Timestamp('2019-10-07 10:30:19.428748+0200', tz='Europe/Brussels')

to convert the timestamp with the tz argument set to the timezone we want to convert to.

We can call tz_localize to convert the timezone to naive local time

pd.Timestamp.now(tz='Europe/Brussels').tz_localize(None)

And we can call tz_convert with None to convert the timestamp with a given time zone to UTC with

pd.Timestamp.now(tz='Europe/Brussels').tz_convert(None) 
Categories
Python Answers

How to convert Python Pandas column containing NaNs to dtype int?

Sometimes, we want to convert Python Pandas column containing NaNs to dtype int, we call astype.

In this article, we’ll look at how to convert Python Pandas column containing NaNs to dtype int, we call astype

How to convert Python Pandas column containing NaNs to dtype int?

To convert Python Pandas column containing NaNs to dtype int, we call astype.

For instance, we write

df[col] = df[col].astype(int)

to call astype with int to convert values in column col to ints.

Conclusion

To convert Python Pandas column containing NaNs to dtype int, we call astype.