Для Python 3.6+
можно использовать очень быстро f-string
s:
z = zip(df['Red'], df['Blue'], df['Green'])
df["Hex"] = [f'#{R:02X}{B:02X}{G:02X}' for R,B,G in z]
Для более низких версий:
df["Hex"] = ['#{0:02X}{1:02X}{2:02X}'.format(R,B,G) for R,B,G in z]
Спасибо @Jon за улучшение решения:
df["Hex"] = ['#{0:02X}{1:02X}{2:02X}'.format(*el) for el in z]
Производительность :
#10000 rows
df = pd.DataFrame(np.random.randint(256, size=(10000, 3)), columns=['Red', 'Green', 'Blue'])
In [244]: %%timeit
...: z = zip(df['Red'], df['Green'], df['Blue'])
...: df["Hex"] = [f'#{R:02X}{B:02X}{G:02X}' for R,B,G in z]
...:
12.9 ms ± 45.2 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
In [245]: %%timeit
...: z = zip(df['Red'], df['Green'], df['Blue'])
...: df["Hex"] = ['#{0:02X}{1:02X}{2:02X}'.format(R,B,G) for R,B,G in z]
...:
12.4 ms ± 1.14 ms per loop (mean ± std. dev. of 7 runs, 100 loops each)
In [246]: %%timeit
...: z = zip(df['Red'], df['Green'], df['Blue'])
...: df["Hex"] = ['#{0:02X}{1:02X}{2:02X}'.format(*el) for el in z]
...:
11.3 ms ± 55 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
In [246]: %%timeit
...: df["Hex"] = df.apply('#{Red:02X}{Green:02X}{Blue:02X}'.format_map, axis=1)
...:
346 ms ± 42.6 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)