PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, August 18, 2022

[FIXED] How to use for loop to convert the values into dataframe?

 August 18, 2022     dataframe, output, pandas, python     No comments   

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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing