将数字和字符串拆分为熊猫上的differents列

时间:2020-10-25 11:04:30

标签: python pandas split

我喜欢将列分为frame_1和数字。

Traceback (most recent call last):
  File "c:/Users/EM/Desktop/Scripts/gui/slideshow_model/slide_show_class.py", line 67, in <module>
    slideshow_model.show_image(img1)
  File "c:/Users/EM/Desktop/Scripts/gui/slideshow_model/slide_show_class.py", line 55, in show_image
    image = ImageTk.PhotoImage(file=resize_image)
  File "C:\Users\EM\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\ImageTk.py", line 89, in __init__
    image = _get_image_from_kw(kw)
  File "C:\Users\EM\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\ImageTk.py", line 58, in _get_image_from_kw
    return Image.open(source)
  File "C:\Users\EM\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\Image.py", line 2852, in open
    prefix = fp.read(16)
AttributeError: 'Image' object has no attribute 'read'
Exception ignored in: <function PhotoImage.__del__ at 0x0000017EAD432D08>
Traceback (most recent call last):
  File "C:\Users\EM\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\ImageTk.py", line 118, in __del__
    name = self.__photo.name
AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo'

我这样做是为了分割数字

import tkinter as tk
from PIL import ImageTk, Image

root = tk.Tk()


class SlideshowModel():

    def __init__(self, master):
        self.master = root
        master.title('Basic Image Viewer')
        root.iconbitmap('../img/favicon.ico')
        root.state('zoomed')
        s_w = int(root.winfo_screenwidth())
        s_h = int(root.winfo_screenheight())
        self.grid_w = s_w // 2
        self.grid_h = s_h // 2
        self.frame_1 = tk.Frame(master, height=self.grid_h,
                                width=self.grid_w, bd=0)
        self.frame_1.grid(column=0, row=0)
        self.can = tk.Canvas(
            self.frame_1, width=(self.grid_w - 50), height=(self.grid_h - 50), bg="red")
        self.can.grid(row=0, column=0)
        self.frame_2 = tk.Frame(master, height=self.grid_h,
                                width=self.grid_w, bd=0, bg="black")
        self.frame_2.grid(column=1, row=0)
        self.frame_3 = tk.Frame(master, height=self.grid_h,
                                width=self.grid_w, bd=0, bg="black")
        self.frame_3.grid(column=0, row=1)
        self.frame_4 = tk.Frame(master, height=self.grid_h,
                                width=self.grid_w, bd=0, bg="black")
        self.frame_4.grid(column=1, row=1)


    # should return the image object
    def resize_image(self, img_path):
        image = Image.open(img_path)
        w_coeff = image.width / self.grid_w
        h_coeff = image.height / self.grid_h
        w_coeff = 1 / w_coeff if w_coeff > 1 else w_coeff
        h_coeff = 1 / h_coeff if h_coeff > 1 else h_coeff
        # pick the smallest coeff to get the image as small
        # as should be
        coeff = min(w_coeff, h_coeff)
        image = image.resize(
            (int(image.width * coeff), int(image.height * coeff)), Image.ANTIALIAS)
        return image

    # this function should show returned image
    # takes: image object, master frame
    def show_image(self, resize_image):
        image = ImageTk.PhotoImage(resize_image)
        # label = tk.Label(frame_x, image=image, bd=0)
        self.can.create_image(0, 0, image=image, anchor='nw')


slideshow_model = SlideshowModel(root)

img1 = slideshow_model.resize_image('../img/sample.jpg')

slideshow_model.show_image(img1)

root.mainloop()


现在我想再增加一列,这样我只得到字符串, 我不知道它是否会影响,但会影响原始数据,而不是英语

类似:

canvas

1 个答案:

答案 0 :(得分:1)

我相信您需要Series.str.extract\D来处理非数字数据,并需要Series.str.strip来删除尾随空格:

df["number"]=df["name&numb"].str.extract('(\d+)')

df["strings"] = df["name&numb"].str.extract('(\D+)', expand=False).str.strip()

如果需要所有字符串,则使用一个主意:

f = lambda x: ' '.join(y for y in x.split() if not y.isdigit())
df["strings1"] = df["name&numb"].apply(f)
print (df)
          name&numb number      strings      strings1
0           cat 123    123          cat           cat
1            34 dog     34          dog           dog
2           bird 93     93         bird          bird
3    dolphin dof 8       8  dolphin dof   dolphin dof
4       lion cat 76     76     lion cat      lion cat
5  tiger 22 animal      22        tiger  tiger animal