Skip to main content
 首页 » 编程设计

python之如何使用正则表达式更改数据记录的顺序并将其放在一个数据框中

2024年08月13日35zlslch

我想知道的是如何使用带有正则表达式的上述数据框来按正确的顺序排列数据行。 正如您通过索引 2 和 4 看到的那样,Quantity 和 Piece 的顺序错误。 有谁知道我该如何解决这个问题?

data = [['Total 8\r\r\nQuantity 2\r\r\nPiece 4'], ['Total 8\r\r\nQuantity 2\r\r\nPiece 4'],['Total 8\r\r\nPiece 2\r\r\nQuantity 4'], ['Total 8\r\r\nQuantity 2\r\r\nPiece 4'], ['Total 8\r\r\nPiece 2\r\r\nQuantity 4'],['Total 8\r\r\nQuantity 2\r\r\nPiece 4'], ['Total 8\r\r\nQuantity 2\r\r\nPiece 4'],['Total 8\r\r\nPiece 2\r\r\nQuantity 4'], ['Total 8\r\r\nQuantity 2\r\r\nPiece 4'], ['Total 8\r\r\nPiece 2\r\r\nQuantity 4']]  
df = pd.DataFrame(data, columns = ['Information'])  
df  
 
+-------+--------------------------------------+ 
| index |             Information              | 
+-------+--------------------------------------+ 
|     0 | Total 8\r\r\nQuantity 2\r\r\nPiece 4 | 
|     1 | Total 8\r\r\nQuantity 2\r\r\nPiece 4 | 
|     2 | Total 8\r\r\nPiece 2\r\r\nQuantity 4 | 
|     3 | Total 8\r\r\nQuantity 2\r\r\nPiece 4 | 
|     4 | Total 8\r\r\nPiece 2\r\r\nQuantity 4 | 
|     5 | Total 8\r\r\nQuantity 2\r\r\nPiece 4 | 
|     6 | Total 8\r\r\nQuantity 2\r\r\nPiece 4 | 
|     7 | Total 8\r\r\nPiece 2\r\r\nQuantity 4 | 
|     8 | Total 8\r\r\nQuantity 2\r\r\nPiece 4 | 
|     9 | Total 8\r\r\nPiece 2\r\r\nQuantity 4 | 
+-------+--------------------------------------+ 
 
 
dt = pd.DataFrame(df) 
data = [] 
for item in dt['Information']: 
    regex = re.findall(r"(\d+)\D+(\d+)\D+(\d+)",item) 
    quantity = re.findall(r"\bTotal\s?\d\D+(\bQuantity)",item) 
    piece = re.findall(r"\bTotal\s?\d\D+(\bPiece)",item) 
    regex = (map(list,regex)) 
    data.append(list(map(int,list(regex)[0]))) 
dftotal = pd.DataFrame(data, columns=['Total','Quantity','Piece']) 
print(dftotal) 

通过这段代码,我得到了如下所示的一列

+-------+----------+-------+ 
| Total | Quantity | Piece | 
+-------+----------+-------+ 
|     8 |        2 |     4 | 
|     8 |        2 |     4 | 
|     8 |        2 |     4 | 
|     8 |        2 |     4 | 
|     8 |        2 |     4 | 
|     8 |        2 |     4 | 
|     8 |        2 |     4 | 
|     8 |        2 |     4 | 
|     8 |        2 |     4 | 
+-------+----------+-------+  

如何通过切换“数据数组”中的错误顺序并将正确的变量放在单个数据框中来获得如下所示的数据框?

+-------+----------+-------+    
| Total | Quantity | Piece | 
+-------+----------+-------+ 
|     8 |        2 |     4 | 
|     8 |        4 |     2 | 
|     8 |        2 |     4 | 
|     8 |        4 |     2 | 
|     8 |        2 |     4 | 
|     8 |        2 |     4 | 
|     8 |        4 |     2 | 
|     8 |        2 |     4 | 
|     8 |        4 |     2 | 
+-------+----------+-------+ 

请您参考如下方法:

这是一种使用 str.extract

的方法

例如:

import pandas as pd 
 
data = [['Total 8\r\r\nQuantity 2\r\r\nPiece 4'], ['Total 8\r\r\nQuantity 2\r\r\nPiece 4'],['Total 8\r\r\nPiece 2\r\r\nQuantity 4'], ['Total 8\r\r\nQuantity 2\r\r\nPiece 4'], ['Total 8\r\r\nPiece 2\r\r\nQuantity 4'],['Total 8\r\r\nQuantity 2\r\r\nPiece 4'], ['Total 8\r\r\nQuantity 2\r\r\nPiece 4'],['Total 8\r\r\nPiece 2\r\r\nQuantity 4'], ['Total 8\r\r\nQuantity 2\r\r\nPiece 4'], ['Total 8\r\r\nPiece 2\r\r\nQuantity 4']]  
df = pd.DataFrame(data, columns = ['Information']) 
 
df["Total"] = df["Information"].str.extract(r"Total (\d+)") 
df["Quantity"] = df["Information"].str.extract(r"Quantity (\d+)") 
df["Piece"] = df["Information"].str.extract(r"Piece (\d+)") 
df.drop("Information", inplace=True, axis=1) 
print(df) 

输出:

  Total Quantity Piece 
0     8        2     4 
1     8        2     4 
2     8        4     2 
3     8        2     4 
4     8        4     2 
5     8        2     4 
6     8        2     4 
7     8        4     2 
8     8        2     4 
9     8        4     2