Categories
Python Answers

How to use Python Pandas with pd.read_excel() for multiple worksheets of the same workbook?

Sometimes, we want to use Python Pandas with pd.read_excel() for multiple worksheets of the same workbook.

In this article, we’ll look at how to use Python Pandas with pd.read_excel() for multiple worksheets of the same workbook.

How to use Python Pandas with pd.read_excel() for multiple worksheets of the same workbook?

To use Python Pandas with pd.read_excel() for multiple worksheets of the same workbook, we can call read_excel with the sheet name.

For instance, we write

xls = pd.ExcelFile('path_to_file.xls')
df1 = pd.read_excel(xls, 'Sheet1')
df2 = pd.read_excel(xls, 'Sheet2')

to read the excelt file with pd.ExcelFile.

Then we call read_excel with the xls file handle and the sheet name.

Then we assign the returned data frame to variables.

Conclusion

To use Python Pandas with pd.read_excel() for multiple worksheets of the same workbook, we can call read_excel with the sheet name.

Categories
Python Answers

How to improve subplot size/spacing with many subplots in Python matplotlib?

To improve subplot size/spacing with many subplots in Python matplotlib, we can use the subplots_adjust method.

For instance, we write

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as tic

fig = plt.figure()

x = np.arange(100)
y = 3.*np.sin(x*2.*np.pi/100.)

for i in range(5):
    temp = 510 + i
    ax = plt.subplot(temp)
    plt.plot(x,y)
    plt.subplots_adjust(hspace = .001)
    temp = tic.MaxNLocator(3)
    ax.yaxis.set_major_locator(temp)
    ax.set_xticklabels(())
    ax.title.set_visible(False)

plt.show()

to call plt.subplots_adjust with the hspace argument to adjust the horizontal spacing.

Categories
Python Answers

How to dynamically evaluate an expression from a formula in Python Pandas?

Sometimes, we want to dynamically evaluate an expression from a formula in Python Pandas.

In this article, we’ll look at how to dynamically evaluate an expression from a formula in Python Pandas.

How to dynamically evaluate an expression from a formula in Python Pandas?

To dynamically evaluate an expression from a formula in Python Pandas, we can use eval.

For instance, we write

np.random.seed(0)
df1 = pd.DataFrame(np.random.choice(10, (5, 4)), columns=list('ABCD'))
df2 = pd.DataFrame(np.random.choice(10, (5, 4)), columns=list('ABCD'))
df3 = pd.DataFrame(np.random.choice(10, (5, 4)), columns=list('ABCD'))
df4 = pd.DataFrame(np.random.choice(10, (5, 4)), columns=list('ABCD'))
x = 5
pd.eval("df1.A + (df1.B * x)")

to create a few datadrames with DataFrame.

Then we call eval with an expression string that gets the values from the dataframes and multiply df1.B by x.

Conclusion

To dynamically evaluate an expression from a formula in Python Pandas, we can use eval.

Categories
Python Answers

How to create multiple dataframes in loop with Python Pandas?

Sometimes, we want to create multiple dataframes in loop with Python Pandas.

In this article, we’ll look at how to create multiple dataframes in loop with Python Pandas..

How to create multiple dataframes in loop with Python Pandas?

To create multiple dataframes in loop with Python Pandas, we can use dictionary comprehension.

For instance, we write

d = {name: pd.DataFrame() for name in companies}

to create data frames with DataFrame from the companies list with with key name with

{name: pd.DataFrame() for name in companies}

and assign the return data frame dictionary to d.

Conclusion

To create multiple dataframes in loop with Python Pandas, we can use dictionary comprehension.

Categories
Python Answers

How to construct a Python Pandas DataFrame from items in nested dictionary?

Sometimes, we want to construct a Python Pandas DataFrame from items in nested dictionary.

In this article, we’ll look at how to construct a Python Pandas DataFrame from items in nested dictionary.

How to construct a Python Pandas DataFrame from items in nested dictionary?

To construct a Python Pandas DataFrame from items in nested dictionary, we can use dictionary comprehension to get the values we want before creating the data frame.

For instance, we write

pd.concat({k: pd.DataFrame(v).T for k, v in user_dict.items()}, axis=0)

to call pd.concat with a fictionary that we create from creating the DataFrame with value v that we get from the key-value pairs returned by items.

Conclusion

To construct a Python Pandas DataFrame from items in nested dictionary, we can use dictionary comprehension to get the values we want before creating the data frame.