Summary of the Chapter: “Working with Several Data Sources”

Slice of data from an external dictionary

When working with slices, you can use external variables not only of a numeric or string type, but also sheets, dictionaries, Series, and even DataFrames. You can address them just as you would with ordinary external variables. If a sheet is used for a slice, then a check is conducted to determine whether or not the value in a certain column is included in the sheet:

our_list = [1, 2, 3]
print(data.query('column in @our_list'))

A similar check is performed using a dictionary. It’s checked whether the value in a certain column is included in the dictionary keys:

our_dict = {0: 10, 1: 11, 2: 12}
print(data.query('column in @our_dict'))

When a Series object is saved in the variable, the column in @our_series construct checks the inclusion of values in the list, rather than indices:

our_series = pd.Series([10,11,12])
print(data.query('column in @our_series'))

If you have to check inclusion in an index, indicate it explicitly by adding * index * after a dot: column in @ our_series.index:

our_series = pd.Series([10,11,12])
print(data.query('column in @our_series.index'))

When working with a DataFrame object, **inclusion in an index is checked in the same way as in a Series: by adding index after a dot to the name of the DataFrame:

our_dataframe = pd.DataFrame ({
'column1': [2, 4, 6],
'column2': [3, 2, 2],
'column3': ['A', 'B', 'C'],
})
print(data.query('column in @our_dataframe.index'))

In order to check inclusion in a column, pass its name:

our_dataframe = pd.DataFrame ({
'column1': [2, 4, 6],
'column2': [3, 2, 2],
'column3': ['A', 'B', 'C'],
})
print(data.query('column in @our_dataframe.column2'))

Adding a column

Several histograms can be displayed on a single chart. In order to do so, you can use the following construct:

ax = data1.plot(kind='hist', y='column1', histtype='step', range=(0, 500), bins=25,
                              linewidth=5, alpha=0.7, label='raw')
data2.plot(kind='hist', y='column1', histtype='step', range=(0, 500), bins=25, 
                       linewidth=5, alpha=0.7, label='filtered', ax=ax, grid=True, legend=True)

Please note that we did not call the hist() method, but rather plot() with the kind parameter, to which the value kind=‘hist’ was set. This will result in almost the same histogram. It supports those parameters that cannot be used with the hist() method: