Python Forum
concat 3 columns of dataframe to one column
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
concat 3 columns of dataframe to one column
#1
Hi,
I've got a dataframe with 100 rows and 3 columns (please see attachment).

How can I concate the 3 columns to one column?

Per example the 3 elements of the first row (255 255 255) should be 1 element (255255255) and so on.

import numpy as np
import pandas as pd
from PIL import Image

colorImage = Image.open("p2.bmp")
colorPixels = colorImage.convert('RGB')
# Make into array
colorArray = np.array(colorPixels.getdata())
df = pd.DataFrame(colorArray)
print(df)
df2 = pd.concat(df["0"], df["1"], df["2"])
print(df2)
Thanks a lot for your help!!

Attached Files

Thumbnail(s)
   
Reply
#2
You cannot put a list, array or tuple in a DataFrame column. Do you want to convert to a string?
import numpy as np
import pandas as pd
from PIL import Image

colorImage = Image.open("test.png").convert("RGB")
colorArray = np.array([f"({r:03}{g:03}{b:03})" for r, g, b in colorImage.getdata()])
df = pd.DataFrame(colorArray)
print(df)
Output:
0 0 (034177076) 1 (237028036) 2 (237028036) . . .
I don't see any value in that, but maybe there is for you.

Are you trying to get a 24 bit color number, or color string?
import numpy as np
import pandas as pd
from PIL import Image

colorImage = Image.open("test.png").convert("RGB")
df = pd.DataFrame(np.array(colorImage.getdata()), columns=["R", "G", "B"])
df["24bit"] = df["R"] * 65536 + df["G"] * 256 + df["B"]
df["Red"] = df["R"].apply(hex)
df["Green"] = df["G"].apply(hex)
df["Blue"] = df["B"].apply(hex)
df["RGB"] = df["24bit"].apply(hex)
print(df)
Output:
R G B 24bit Red Green Blue RGB 0 34 177 76 2273612 0x22 0xb1 0x4c 0x22b14c 1 237 28 36 15539236 0xed 0x1c 0x24 0xed1c24 2 237 28 36 15539236 0xed 0x1c 0x24 0xed1c24
If you're only using pandas to try to make a 24bit pixel you can do that in numpy.
import numpy as np
from PIL import Image

rgb = np.array(Image.open("test.png").convert("RGB").getdata())
b24 = np.apply_along_axis(lambda p: p[0] * 65536 + p[1] * 256 + p[2], 1, rgb)
print([hex(pixel) for pixel in b24])
Output:
['0x22b14c', '0xed1c24', '0xed1c24', '0xed1c24', '0xed1c24', '0xed1c24', '0xed1c24', '0xed1c24',...
Or if you just one those goofy strings from the first example, you don't need pandas or numpy.
from PIL import Image

b24 = [
    f"({r:03}{g:03}{b:03})"
    for r, g, b in Image.open("test.png").convert("RGB").getdata()
]
print(b24)
Output:
['(034177076)', '(237028036)', '(237028036)', '(237028036)', '(237028036)', '(237028036)',...
Reply
#3
Hi,

Thanks a lot for your answer!

I will describe in detail what I want to achieve tomorrow.

Greetings...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to add columns to polars dataframe sayyedkamran 1 1,811 Nov-03-2023, 03:01 PM
Last Post: gulshan212
  HTML Decoder pandas dataframe column mbrown009 3 1,063 Sep-29-2023, 05:56 PM
Last Post: deanhystad
  attempt to split values from within a dataframe column mbrown009 8 2,383 Apr-10-2023, 02:06 AM
Last Post: mbrown009
  New Dataframe Column Based on Several Conditions nb1214 1 1,823 Nov-16-2021, 10:52 PM
Last Post: jefsummers
  Putting column name to dataframe, can't work. jonah88888 1 1,845 Sep-28-2021, 07:45 PM
Last Post: deanhystad
  Setting the x-axis to a specific column in a dataframe devansing 0 2,043 May-23-2021, 12:11 AM
Last Post: devansing
  Convert several columns to int in dataframe Krayna 2 2,426 May-21-2021, 08:55 AM
Last Post: Krayna
Question [Solved] How to refer to dataframe column name based on a list lorensa74 1 2,281 May-17-2021, 07:02 AM
Last Post: lorensa74
  iretate over columns in df and calculate euclidean distance with one column in pandas Pit292 0 3,314 May-09-2021, 06:46 PM
Last Post: Pit292
  Outputs "NaN" after "DataFrame columns" function? epsilon 7 3,695 Jan-27-2021, 10:59 AM
Last Post: epsilon

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020