I am working with a relatively large dataset (in Python with Pandas) and am trying to build combinations of multiple columns as a string.
Let's say I have two lists; x
and y
, where: x = ["sector_1", "sector_2", "sector_3", ...]
and y = [7, 19, 21, ...]
.
I have been using a for
loop to build combinations such that combined = ["sector_1--7", "sector_1--19", "sector_1--21", "sector_2--7", "sector_2--19", ...]
, with the separator here defined as --
.
My current code looks like this:
sep = '--'
combined = np.empty(0, dtype='object')
for x_value in x:
for y_value in y:
combined = np.append(combined, str(x_value) + sep + str(y_value))
combined = pd.DataFrame(combined)
combined = combined.iloc[:, 0].str.split(sep, expand=True)
The code above works but I was just wondering if there was a better way (perhaps more efficient in runtime).