Issue
I am using several functions and i am using them to print values but now I need further calculation. So, I need to convert the printing values in a special format.
This is how my code looks:
for color in dominant_colors.colors:
print('\tred: {0}'.format(color.color.red))
print('\tgreen: {0}'.format(color.color.green))
print('\tblue: {0}'.format(color.color.blue))
print('')
and its printing values like this:
red: 232.0
green: 179.0
blue: 124.0
red: 160.0
green: 152.0
blue: 150.0
red: 85.0
green: 82.0
blue: 81.0
But now I want the values in RGB format and that should be like this: (232, 179, 124)
So then I would like to get the column as dataframe which will give output like this:
Colors
(232,179,124)
(160,152,150)
(85,82,81)
Solution
Use list comprehension for list of tuples:
L = [(color.color.red, color.color.green, color.color.blue) for color in dominant_colors.colors]
Or loop solution with append
:
L = []
for color in dominant_colors.colors:
L.append((color.color.red, color.color.green, color.color.blue))
And then pass to DataFrame
constructor:
df = pd.DataFrame({'Colors':L})
Answered By - jezrael Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.