正则表达式使用正则表达式中的变量搜索列中的单词,返回匹配的单词和变量nam

2024-06-12 16:33:31 发布

您现在位置:Python中文网/ 问答频道 /正文

基本上,我试图从数据帧的同一行的循环中获得正则表达式匹配的单词+单词,如果这有意义的话

我试着用列名创建列表,然后快速浏览,但没有成功。我也不知道如何在re.findall中使用变量名和regex,否则我会尝试以下方法:

result = pd.DataFrame()
for word in file2:
   x = re.findall(word.*, file1)
result = result.append(x, word) 

我知道上面的代码不起作用,可能有一些原因,我真的很想解释一下为什么上面的模拟代码不起作用,以及如何让它起作用。。。同时,我提出了以下建议,其中一半有效:

import pandas as pd
from pandas import read_csv

file1 = pd.DataFrame(read_csv('alldatacols.csv'))
file2 = pd.DataFrame(read_csv('trainColumnsT2.csv'))

new_df = pd.DataFrame()

for word in file2['Name']:
    y = file1['0'].str.extractall(r'({}.*)'.format(word))
    new_df = new_df.append(y, ignore_index=True)
new_df.reset_index(drop=True)
print(new_df)
print(new_df.columns)

结果:

0                       Id
1               MSSubClass
2         MSZoning_C (all)
3              MSZoning_FV
4              MSZoning_RH
5              MSZoning_RL
6              MSZoning_RM
7              LotFrontage
8                  LotArea
9              Street_Grvl
10             Street_Pave
11              Alley_Grvl
12              Alley_Pave
13            LotShape_IR1
14            LotShape_IR2
15            LotShape_IR3
16            LotShape_Reg
17         LandContour_Bnk
18         LandContour_HLS
19         LandContour_Low
20         LandContour_Lvl
21        Utilities_AllPub
22        Utilities_NoSeWa
23        LotConfig_Corner
24       LotConfig_CulDSac
25           LotConfig_FR2
26           LotConfig_FR3
27        LotConfig_Inside
28           LandSlope_Gtl
29           LandSlope_Mod
..                     ...
266               PoolArea
267              PoolQC_Ex
268              PoolQC_Fa
269              PoolQC_Gd
270            Fence_GdPrv
271             Fence_GdWo
272            Fence_MnPrv
273             Fence_MnWw
274       MiscFeature_Gar2
275       MiscFeature_Othr
276       MiscFeature_Shed
277       MiscFeature_TenC
278                MiscVal
279                 MoSold
280                 YrSold
281           SaleType_COD
282           SaleType_CWD
283           SaleType_Con
284         SaleType_ConLD
285         SaleType_ConLI
286         SaleType_ConLw
287           SaleType_New
288           SaleType_Oth
289            SaleType_WD
290  SaleCondition_Abnorml
291  SaleCondition_AdjLand
292   SaleCondition_Alloca
293   SaleCondition_Family
294   SaleCondition_Normal
295  SaleCondition_Partial

我正在寻找的示例输出:

17         LandContour_Bnk      LandContour
18         LandContour_HLS      LandContour
19         LandContour_Low      LandContour
20         LandContour_Lvl      LandContour
274       MiscFeature_Gar2      MiscFeature
275       MiscFeature_Othr      MiscFeature
276       MiscFeature_Shed      MiscFeature
277       MiscFeature_TenC      MiscFeature
281           SaleType_COD      SaleType
282           SaleType_CWD      SaleType
283           SaleType_Con      SaleType
284         SaleType_ConLD      SaleType
285         SaleType_ConLI      SaleType
286         SaleType_ConLw      SaleType
287           SaleType_New      SaleType
288           SaleType_Oth      SaleType
289            SaleType_WD      SaleType
290  SaleCondition_Abnorml      SaleCondition
291  SaleCondition_AdjLand      SaleCondition
292   SaleCondition_Alloca      SaleCondition
293   SaleCondition_Family      SaleCondition
294   SaleCondition_Normal      SaleCondition
295  SaleCondition_Partial      SaleCondition

请帮助我了解如何克服困难。谢谢大家!


Tags: csvdataframedfnewresultwordpdfence
1条回答
网友
1楼 · 发布于 2024-06-12 16:33:31

所以我制作了另一个数据框,我只搜索单词,不搜索。*。然后我连接了两个数据帧,得到了我需要的。。。这是一个简单的解决方案,在过去的7个小时里,当我尝试其他方法时,我想不出来

file1 = pd.DataFrame(read_csv('alldatacols.csv'))
file2 = pd.DataFrame(read_csv('trainColumnsT2.csv'))

new_df = pd.DataFrame()
**df = pd.DataFrame()**
for word in file2['Name']:

    y = file1['0'].str.extractall(r'({}.*)'.format(word))
    **x = file1['0'].str.extractall(r'({})'.format(word))**
    new_df = new_df.append(y, ignore_index=True)
    **df = df.append(x, ignore_index=True)**

new_df.reset_index(drop=True)
**df.reset_index(drop=True)
result = pd.concat([new_df, df], axis=1)**

result.to_csv('result.csv')

相关问题 更多 >