如何在数据框中分割此文本文件?

2024-06-01 15:37:38 发布

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

我有一个.txt文件

01 DEZ Acom Mkt Place - 3/3 85,60
01 DEZ Magalu Com *Magazinel - 8/10 254,90
01 DEZ Amazon.Com.Br - 2/2 74,99
01 DEZ Casa dos Controles - 2/3 59,00
01 DEZ Pagamento em 01 DEZ 1.699,20
04 DEZ Amazonprimebr 9,90
06 DEZ Pagamento em 06 DEZ 701,30
07 DEZ Consilio Engenharia - 1/2 125,00
07 DEZ Crunchyroll 32,00
07 DEZ 99 *99* Pop 05dez 08h5 3,24
08 DEZ Amazon-Marketplace 35,99
08 DEZ Amazon.Com.Br 5,46
10 DEZ Folhadespaulo 1,90
11 DEZ Google Storage 6,99
11 DEZ Google Youtubepremium 12,50
12 DEZ Amazon-Marketplace 33,40
14 DEZ Extrafarma Mariz e Bar 17,89
14 DEZ Uberbr Uber * Pending 8,11
14 DEZ Ebanx *Spotify 8,50
15 DEZ Uberbr Uber * Pending 7,56
15 DEZ Uberbr Uber * Pending 11,77
15 DEZ Uberbr Uber * Pending 14,38
15 DEZ Uberbr Uber * Pending 11,27
15 DEZ Sosthenes & Sosthenes 44,00
16 DEZ Editora Vestseller - 1/2 53,55
18 DEZ Udemy.Com 24,90
26 DEZ Pagamento em 26 DEZ 100,00

我想创建一个包含Date、Product和Value列的数据框架。所以,问题是我如何分割行以将其划分为列。我想如果有任何方法可以找到行中的最后一个空格,但是这个值是可变的,我不知道这是否是最好的方法

谢谢你的帮助


Tags: 文件方法brtxtcomamazongooglepagamento
1条回答
网友
1楼 · 发布于 2024-06-01 15:37:38

这是一个简单的正则表达式。显然,我用于日期的表达式非常懒惰

df = pd.DataFrame("""01 DEZ Acom Mkt Place - 3/3 85,60
01 DEZ Magalu Com *Magazinel - 8/10 254,90
01 DEZ Amazon.Com.Br - 2/2 74,99
01 DEZ Casa dos Controles - 2/3 59,00
01 DEZ Pagamento em 01 DEZ 1.699,20
04 DEZ Amazonprimebr 9,90
06 DEZ Pagamento em 06 DEZ 701,30
07 DEZ Consilio Engenharia - 1/2 125,00
07 DEZ Crunchyroll 32,00
07 DEZ 99 *99* Pop 05dez 08h5 3,24
08 DEZ Amazon-Marketplace 35,99
08 DEZ Amazon.Com.Br 5,46
10 DEZ Folhadespaulo 1,90
11 DEZ Google Storage 6,99
11 DEZ Google Youtubepremium 12,50
12 DEZ Amazon-Marketplace 33,40
14 DEZ Extrafarma Mariz e Bar 17,89
14 DEZ Uberbr Uber * Pending 8,11
14 DEZ Ebanx *Spotify 8,50
15 DEZ Uberbr Uber * Pending 7,56
15 DEZ Uberbr Uber * Pending 11,77
15 DEZ Uberbr Uber * Pending 14,38
15 DEZ Uberbr Uber * Pending 11,27
15 DEZ Sosthenes & Sosthenes 44,00
16 DEZ Editora Vestseller - 1/2 53,55
18 DEZ Udemy.Com 24,90
26 DEZ Pagamento em 26 DEZ 100,00""".split("\n"), columns=["src"])

df[["Date","Product","Value"]] = df["src"].str.extract(r"([0-9]+ [A-Z]+) (.*) ([0-9,]+)")
print(df.to_string(index=False))

输出

                                        src    Date                       Product   Value
          01 DEZ Acom Mkt Place - 3/3 85,60  01 DEZ          Acom Mkt Place - 3/3   85,60
 01 DEZ Magalu Com *Magazinel - 8/10 254,90  01 DEZ  Magalu Com *Magazinel - 8/10  254,90
           01 DEZ Amazon.Com.Br - 2/2 74,99  01 DEZ           Amazon.Com.Br - 2/2   74,99
      01 DEZ Casa dos Controles - 2/3 59,00  01 DEZ      Casa dos Controles - 2/3   59,00
        01 DEZ Pagamento em 01 DEZ 1.699,20  01 DEZ           Pagamento em 01 DEZ       1
                  04 DEZ Amazonprimebr 9,90  04 DEZ                 Amazonprimebr    9,90
          06 DEZ Pagamento em 06 DEZ 701,30  06 DEZ           Pagamento em 06 DEZ  701,30
    07 DEZ Consilio Engenharia - 1/2 125,00  07 DEZ     Consilio Engenharia - 1/2  125,00
                   07 DEZ Crunchyroll 32,00  07 DEZ                   Crunchyroll   32,00
         07 DEZ 99 *99* Pop 05dez 08h5 3,24  07 DEZ        99 *99* Pop 05dez 08h5    3,24
            08 DEZ Amazon-Marketplace 35,99  08 DEZ            Amazon-Marketplace   35,99
                  08 DEZ Amazon.Com.Br 5,46  08 DEZ                 Amazon.Com.Br    5,46
                  10 DEZ Folhadespaulo 1,90  10 DEZ                 Folhadespaulo    1,90
                 11 DEZ Google Storage 6,99  11 DEZ                Google Storage    6,99
         11 DEZ Google Youtubepremium 12,50  11 DEZ         Google Youtubepremium   12,50
            12 DEZ Amazon-Marketplace 33,40  12 DEZ            Amazon-Marketplace   33,40
        14 DEZ Extrafarma Mariz e Bar 17,89  14 DEZ        Extrafarma Mariz e Bar   17,89
          14 DEZ Uberbr Uber * Pending 8,11  14 DEZ         Uberbr Uber * Pending    8,11
                 14 DEZ Ebanx *Spotify 8,50  14 DEZ                Ebanx *Spotify    8,50
          15 DEZ Uberbr Uber * Pending 7,56  15 DEZ         Uberbr Uber * Pending    7,56
         15 DEZ Uberbr Uber * Pending 11,77  15 DEZ         Uberbr Uber * Pending   11,77
         15 DEZ Uberbr Uber * Pending 14,38  15 DEZ         Uberbr Uber * Pending   14,38
         15 DEZ Uberbr Uber * Pending 11,27  15 DEZ         Uberbr Uber * Pending   11,27
         15 DEZ Sosthenes & Sosthenes 44,00  15 DEZ         Sosthenes & Sosthenes   44,00
      16 DEZ Editora Vestseller - 1/2 53,55  16 DEZ      Editora Vestseller - 1/2   53,55
                     18 DEZ Udemy.Com 24,90  18 DEZ                     Udemy.Com   24,90
          26 DEZ Pagamento em 26 DEZ 100,00  26 DEZ           Pagamento em 26 DEZ  100,00

相关问题 更多 >