亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何提取 pandas 數據框單元格中的字符串的一部分并創建一個包含該字符串的新列

如何提取 pandas 數據框單元格中的字符串的一部分并創建一個包含該字符串的新列

慕桂英3389331 2023-07-27 14:03:55
我有一個數據框,其中一列包含一個很長的字符串,其中包含很多信息,我需要將這些信息分解為單獨的列并將它們添加到數據框中。我可以創建空列,但我不知道字符串是否可以提取元素或者是否可以將其分成列。例如數據行0????Row?1?Ch475?Vi?17.0V?BF27?Sclk?100ns?1in24?24segs所需輸出行號、伏特、Wfm、Sclk、圖像、段1 , 17 , BF27 , 100 , 1in24 , 24數據? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Comments? Image0? ? Row 1 Ch475 Vi 17.0V BF27 Sclk 100ns 1in24 24segs? ? ? 01? ? Row 1 Ch475 Vi 17.0V BF27 Sclk 100ns 1in24 24segs? ? ? 02? ? Row 1 Ch475 Vi 17.0V BF27 Sclk 100ns 1in24 24segs? ? ? 03? ? Row 1 Ch475 Vi 17.0V BF27 Sclk 100ns 1in24 24segs? ? ? 04? ? Row 1 Ch475 Vi 17.0V BF27 Sclk 100ns 1in24 24segs? ? ? 0..? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?...? ? ...706? Row 2 Ch475 Vi 17.5V BF27 Sclk 100ns 1in24 24segs? ? ? 0707? Row 2 Ch475 Vi 17.5V BF27 Sclk 100ns 1in24 24segs? ? ? 0708? Row 2 Ch475 Vi 17.5V BF27 Sclk 100ns 1in24 24segs? ? ? 0709? Row 2 Ch475 Vi 17.5V BF27 Sclk 100ns 1in24 24segs? ? ? 0710? Row 2 Ch475 Vi 17.5V BF27 Sclk 100ns 1in24 24segs? ? ? 0代碼import pandas as pdimport numpy as nppath = "/Users/.../Desktop/tk_gui_grid/"file = "orig_data.txt"filepath = path+filedf = pd.read_csv(filepath, sep='\t', lineterminator='\r')com = df.loc[:,['Comments']]dfLen = len(com)image = [0]*dfLencom['Image'] = imageprint(com)
查看完整描述

2 回答

?
神不在的星期二

TA貢獻1963條經驗 獲得超6個贊

這是使用正則表達式和命名捕獲組的快速解決方案。


正則表達式的優點split:

有些人評論說不需要正則表達式,這是一個真實的說法。然而,從數據驗證的角度來看,使用正則表達式有助于防止“雜散”數據悄悄進入。使用“盲”函數將split()數據分割為(一個字符);但如果源數據發生了變化怎么辦?該split函數對此是盲目的。然而,使用正則表達式將有助于突出顯示問題,因為模式根本不匹配。是的,您可能會收到一條錯誤消息 - 但這是一件好事,因為您會收到數據格式更改的警報,從而提供解決問題或更新正則表達式模式的機會。


來源數據:

模擬額外的行以進行演示。


0    Row 1 Ch475 Vi 17.0V BF27 Sclk 100ns 1in24 24segs

1    Row 2 Ch475 Vi 17.1V BF27 Sclk 101ns 1in24 25segs

2    Row 3 Ch475 Vi 17.2V BF27 Sclk 102ns 1in24 26segs

3    Row 4 Ch475 Vi 17.3V BF27 Sclk 103ns 1in24 27segs

4    Row 5 Ch475 Vi 17.4V BF27 Sclk 104ns 1in24 28segs

代碼:

import pandas as pd

import re


path = './orig_data.txt'

cols = ['rownumber', 'volts', 'wfm', 'sclk', 'image', 'segment']

exp = re.compile(r'^\d+\s+Row\s'

                 r'(?P<rownumber>\d+).*\s'

                 r'(?P<volts>\d+\.\d+)V\s'

                 r'(?P<wfm>\w+)\sSclk\s'

                 r'(?P<sclk>\d+)ns\s'

                 r'(?P<image>\w+)\s'

                 r'(?P<segment>\d+)segs.*$')


df = pd.read_csv(path, sep='|', header=None, names=['comment'])

df[cols] = df['comment'].str.extract(exp, expand=True)

輸出:

                                             comment rownumber volts   wfm  \

0  0    Row 1 Ch475 Vi 17.0V BF27 Sclk 100ns 1in2...         1  17.0  BF27   

1  1    Row 2 Ch475 Vi 17.1V BF27 Sclk 101ns 1in2...         2  17.1  BF27   

2  2    Row 3 Ch475 Vi 17.2V BF27 Sclk 102ns 1in2...         3  17.2  BF27   

3  3    Row 4 Ch475 Vi 17.3V BF27 Sclk 103ns 1in2...         4  17.3  BF27   

4  4    Row 5 Ch475 Vi 17.4V BF27 Sclk 104ns 1in2...         5  17.4  BF27   


  sclk  image segment  

0  100  1in24      24  

1  101  1in24      25  

2  102  1in24      26  

3  103  1in24      27  

4  104  1in24      28


查看完整回答
反對 回復 2023-07-27
?
胡說叔叔

TA貢獻1804條經驗 獲得超8個贊

您需要將 Series obj 轉換為字符串,然后將其拆分。之后您可以通過索引訪問每個元素


df['Comments'].str.split(' ')


0    [Row, 1, Ch475, Vi, 17.0V, BF27, Sclk, 100ns, ...


df['Comments'].str.split(' ').str[0]


Out[7]: 

0    Row


df['Comments'].str.split(' ').str[4]


Out[8]: 

0    17.0V

如果您了解如何訪問拆分中的每一列,您可以將其分配給數據框中的新行,例如:


df['RowNumber'] = df['Comments'].str.split(' ').str[1]

df['Volts'] = df['Comments'].str.split(' ').str[4]


查看完整回答
反對 回復 2023-07-27
  • 2 回答
  • 0 關注
  • 200 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號