Here is another approach which provides support for both key and column filtering. The solution consists of two functions:
as_dict(df, cols, ids, key): returns data into a dictionary
extract_col_from_dict(dct, col, ids): extracts the column data from a dictionary
Initially let's extract the desired data into a dictionary from the given dataframe:
def as_dict(df, cols = [], ids = [], key = 0):
key_idx = 0
if isinstance(key, int):
key_idx = key
key = df.columns[key_idx]
elif isinstance(key, str):
key_idx = df.columns.index(key)
else:
raise Exception("Please provide a valid key e.g:{1, 'col1'}")
df = df.select("*") if not cols else df.select(*[[key] + cols])
if ids:
df = df.where(df[key].isin(ids))
return df.rdd.map(lambda x : (x[key_idx], x.asDict())).collectAsMap()
Arguments:
- df: the dataframe
- cols: the columns that you want to work with, default include all columns
- ids: in order to avoid collecting all the dataset on the driver you can filter based on this. This applies for the key column. Default include all records
- key: the key column, it can be string/int, default 0
Let's call the function with your dataset:
df = spark.createDataFrame(
[(1, 0.0, 0., 0.5),
(2, 1.0, 0.8, 1.7),
(3, 2.0, 1.6, 2.5),
(4, 4.0, 3.7, 4.7),
(5, 6.0, 5.7, 6.3)], ["bin", "median", "min", "end"])
dict_ = as_dict(df)
dict_
{1: {'bin': 1, 'min': 0.0, 'end': 0.5, 'median': 0.0},
2: {'bin': 2, 'min': 0.8, 'end': 1.7, 'median': 1.0},
3: {'bin': 3, 'min': 1.6, 'end': 2.5, 'median': 2.0},
4: {'bin': 4, 'min': 3.7, 'end': 4.7, 'median': 4.0},
5: {'bin': 5, 'min': 5.7, 'end': 6.3, 'median': 6.0}}
# or with filters applied
dict_ = as_dict(df, cols = ['min', 'end'], ids = [1, 2, 3])
dict_
{1: {'bin': 1, 'min': 0.0, 'end': 0.5},
2: {'bin': 2, 'min': 0.8, 'end': 1.7},
3: {'bin': 3, 'min': 1.6, 'end': 2.5}}
The function will map the records to key/value pairs where the value will be also a dictionary (calling row.asDict).
After calling as_dict function the data will be located on the driver and now you can extract the data that you need with the extract_col_from_dict:
def extract_col_from_dict(dct, col, ids = []):
filtered = {}
if ids:
filtered = { key:val for key, val in dct.items() if key in ids }
else:
filtered = { key:val for key, val in dct.items() }
return [d[col] for d in list(filtered.values())]
Arguments:
- dct: the source dictionary
- col: column to be extracted
- ids: more filtering, default all records
And the output of the function:
min_data = extract_col_from_dict(dict_, 'min')
min_data
[0.0, 0.8, 1.6, 3.7, 5.7]