OD1995
OD1995

Reputation: 1777

Create Hex column from Red column, Green column & Blue column in pandas

I have a pandas data frame with 16,777,216 rows. This is every possible combination of three columns (Red, Green and Blue) between 0 and 255 inclusive.

I would like to add a column to this data frame which is the hex code of the three values of the row. I thought something like the below would have been the best solution:

df["Hex"] = "#{0:02x}{1:02x}{2:02x}".format(df["Red"],df["Green"],df["Blue"])

However, it appears you can't pass a series into the string format method.

Is there a way of getting around this problem? Furthermore, would that be the most efficient way of doing it, given the data frame is fairly large?

Upvotes: 3

Views: 202

Answers (2)

jezrael
jezrael

Reputation: 863421

For python 3.6+ is possible use very fast f-strings:

z = zip(df['Red'], df['Blue'], df['Green'])
df["Hex"] = [f'#{R:02X}{B:02X}{G:02X}' for R,B,G in z]

For lower versions:

df["Hex"] = ['#{0:02X}{1:02X}{2:02X}'.format(R,B,G) for R,B,G in z]

Thank you @Jon for improving solution:

df["Hex"] = ['#{0:02X}{1:02X}{2:02X}'.format(*el) for el in z]

Performance:

#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)

Upvotes: 1

Jon Clements
Jon Clements

Reputation: 142216

You can use .apply, eg:

df = pd.DataFrame(np.random.randint(256, size=(10, 3)), columns=['Red', 'Green', 'Blue'])

eg:

   Red  Green  Blue
0  125    100   174
1  107    247   235
2  230    254    33
3   91    107    33
4  209    220   232
5  175     10    47
6  120     66    44
7   21    136   254
8  226    237    32
9   89     57    71

Then:

df.apply('#{Red:02X}{Green:02X}{Blue:02X}'.format_map, axis=1)

Gives you:

0    #7D64AE
1    #6BF7EB
2    #E6FE21
3    #5B6B21
4    #D1DCE8
5    #AF0A2F
6    #78422C
7    #1588FE
8    #E2ED20
9    #593947
dtype: object

Upvotes: 2

Related Questions