извлечение символов из строк и формирование новых столбцов в Python - PullRequest
0 голосов
/ 26 мая 2018

У меня есть такой DataFrame Pandas:

      Date                   Descriptive          
0  2017-1-1    Time12:30 Id124562 American electronic commerce and cloud computing company based in Seattle     
1  2017-1-2    Time12:40 Id124565 Amazon has separate retail websites for the United States
2  2017-1-3    Time12:45 Id124561 In 2020, Amazon will build a new downtown Seattle building 

Как мне сгенерировать новый DataFrame с помощью Python?

         Date        time      id           descriptive
    0  2017-1-1     12:30    124562     American electronic commerce and cloud computing company based in Seattle     
    1  2017-1-2     12:40    124565     Amazon has separate retail websites for the United States
    2  2017-1-3     12:45    124561     In 2020, Amazon will build a new downtown Seattle building 

PS: Извините, я создаю этот кадрпредставляют реальную проблему очистки данных, которую я встретил.Длина идентификатора установлена ​​на 6. Большое спасибо.

Ответы [ 3 ]

0 голосов
/ 26 мая 2018

join с split с использованием expand=True

cols = ['Time', 'Type', 'Price', 'Id']

df.join(
    pd.DataFrame(
        df.Descriptive.str.replace(
            r'(?:{})([^\s]+)'.format('|'.join(cols)),
            r'\1'
            ).str.split(expand=True).values,
            columns = cols
        )
)

# Result

       Date                            Descriptive   Time Type Price        Id
0  2017-1-1    Time12:30 Type021 Price11$ Id124562  12:30  021   11$    124562
1  2017-1-2  Time12:40 Type011 Price11$ Id12456512  12:40  011   11$  12456512
2  2017-1-3  Time12:45 Type031 Price11$ Id12456125  12:45  031   11$  12456125
0 голосов
/ 26 мая 2018

Новый ответ

epat = re.compile('(\w+?)(\d\S*)')

df.join(pd.DataFrame([dict(re.findall(epat, y)) for y in df.Descriptive], df.index))

       Date                            Descriptive        Id Price   Time Type
0  2017-1-1    Time12:30 Type021 Price11$ Id124562    124562   11$  12:30  021
1  2017-1-2  Time12:40 Type011 Price11$ Id12456512  12456512   11$  12:40  011
2  2017-1-3  Time12:45 Type031 Price11$ Id12456125  12456125   11$  12:45  031

Я просто считаю, что это имя группы соответствия шаблону регулярного выражения имеет вид elegent

time = 'Time(?P<Time>\d{1,2}:\d{2}) '
typ_ = 'Type(?P<Type>\d+) '
prc_ = 'Price(?P<Price>\d+)\$ '
id__ = 'Id(?P<Id>\d+)$'
pat = f'{time}{typ_}{prc_}{id__}'
df.join(df.Descriptive.str.extract(pat, expand=True))

       Date                            Descriptive   Time Type Price        Id
0  2017-1-1    Time12:30 Type021 Price11$ Id124562  12:30  021    11    124562
1  2017-1-2  Time12:40 Type011 Price11$ Id12456512  12:40  011    11  12456512
2  2017-1-3  Time12:45 Type031 Price11$ Id12456125  12:45  031    11  12456125
0 голосов
/ 26 мая 2018

Вы можете попробовать что-то вроде ниже:

items = ['Time', 'Type', 'Price', 'Id']
for index, item in enumerate(items):
    df[item] = df['Descriptive'].apply(lambda row: row.split(' ')[index].split(item)[1])

print(df)

Результат:

       Date                            Descriptive   Time Type Price        Id
0  2017-1-1    Time12:30 Type021 Price11$ Id124562  12:30  021   11$    124562
1  2017-1-2  Time12:40 Type011 Price11$ Id12456512  12:40  011   11$  12456512
2  2017-1-3  Time12:45 Type031 Price11$ Id12456125  12:45  031   11$  12456125

Если цикл for сбивает с толку, вы можете попробовать применить без цикла:

df['Time'] = df['Descriptive'].apply(lambda row: row.split(' ')[0].split('Time')[1])
df['Type'] = df['Descriptive'].apply(lambda row: int(row.split(' ')[1].split('Type')[1]))
df['Price'] = df['Descriptive'].apply(lambda row: row.split(' ')[2].split('Price')[1])
df['Id'] = df['Descriptive'].apply(lambda row: row.split(' ')[3].split('Id')[1])
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...