【问题标题】:Python Split output column with fixed & dynamic length具有固定和动态长度的Python拆分输出列
【发布时间】:2020-10-07 15:43:42
【问题描述】:

我想将数据框从单列拆分为三列Sample input and output [(Col1=固定长度),(Col2=动态长度),(Col3=剩余部分)]

import re
import pandas as pd
text='Raw Data'
out = re.findall(r"RIY-[A-Z]{6}-\d{6}\.\d{6,8}\.\d{5,7}", text) 
df = pd.DataFrame(out, columns = ["RIY"]) 

df["col1"] = df.RIY.str[0:15]
df["col2"] = df.RIY.str[15:24]# need to split based on criteria (find next '.' less 2 char
df["col3"] = df.RIY.str[24:] # remaining all text after splitting 2 column

#Output
[1]: https://i.stack.imgur.com/Lupcd.png

我尝试使用固定长度(Roy2012 的解决方案)进行拆分,该长度只能完美运行,对于第一部分 [0:15],其余两列的长度会有所不同。我想通过找到第二个点('.')少(-2)(以避免删除46)来拆分我想通过(找到第二个点(.)-2(以避免删除46)然后拆分来实现。

【问题讨论】:

    标签: python regex pandas numpy


    【解决方案1】:

    这对你有用吗?

    df.RAW.str.extract(r"(.*)(\d\d\.\d+)(\d\d\.\d+)")
    

    我得到的输出是:

                     0          1          2
    0  RIY-OUHOMH-1002  24.534768  46.650127
    1  RIY-OUHOHH-1017   24.51472  46.663988
    2  RIY-OUHOMH-1004  24.532244  46.651758
    3  RIY-OUHOHH-1007  24.529029  46.653571
    4  RIY-OUHOHH-1006  24.530071  46.651934
    5  RIY-OUHOHH-1005  24.531786   46.65279
    6  RIY-OUHOMH-1001  24.535972  46.649456
    7  RIY-DIRAHH-0151  24.495407  46.641877
    8  RIY-DIRAHH-0152  24.494105  46.644253
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-29
      相关资源
      最近更新 更多