Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 421
OD1995 Avatar asked Dec 15 '25 05:12

OD1995


1 Answers

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
like image 107
Jon Clements Avatar answered Dec 16 '25 21:12

Jon Clements



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!