Cannot figure out how can I add a string result ('color_hek') back to the dataframe (df) as a separate column?
#importing libraries
import pandas as pd
#creating dataset
data = [[0,0,0], [0,0,0]]
df = pd.DataFrame(data, columns = ['red', 'green', 'blue'])
#defining function
def rgb_to_hex(red, green, blue):
"""Return color as #rrggbb for the given color values."""
return '#%02x%02x%02x' % (red, green, blue)
#looping through the dataframe to apply the function
for index, row in df.iterrows():
color_hek = rgb_to_hex(row['red'].astype(int),row['green'].astype(int),row['blue'].astype(int))
print(color_hek)