DataFrame это возможно для очистки / Поиск ключа в том же кадре данных? - PullRequest
0 голосов
/ 01 сентября 2018

Я изучаю Data Science и у меня проблемы с фреймом данных, вы можете мне помочь? В моем dataFrame у меня есть 4 столбца: «Цена», «Местоположение», «Дом с», «Описание». в «Прайсе» и «Дом с» у меня есть несколько рядов с Наном или без него. Я действительно хочу создать функцию, которая выполняет очистку в столбце «Описание», берет ключ (слово, например: 40 долларов или бассейн, сад), и эти ключи переносят в столбцы «цена» или «дом с». , Пример

import pandas as pd
import numpy as np
Df2= {
    'Price': ['90','NaN','NaN',' '],
    'Location': ['NaN','Argentina','NaN','EEUU'],
    'House with': ['Swimming pool', 'Garden','NaN', 'NaN'],
    'Description': ['This house in Brazil cost $90 and       have swimming pool', 'his house in Argentina cost $50 and        have Garden','This house in Chile cost $70 and have Garden', 'This house in EEuu cost $80 and        have swimming pool']}

df3 = pd.DataFrame(Df2)
df3

и мне бы хотелось, чтобы это было так

Df2= {
        'Price': ['90','50','70','80'],
        'Location': ['Brazil','Argentina','Chile','EEUU'],
        'House with': ['Swimming pool', 'Garden','Garden', 'swimming pool'],
        'Description': ['This house in Brazil cost $90 and       have swimming pool', 'his house in Argentina cost $50 and        have Garden','This house in Chile cost $70 and have Garden', 'This house in EEuu cost $80 and        have swimming pool']}

1 Ответ

0 голосов
/ 01 сентября 2018

Вы можете extract групп в строке. Если в серии есть следующие строки:

df['Price'] = df['Description'].str.extract(r'\$(\d+)')[0]
df['Location'] = df['Description'].str.extract(r'house in ([A-Za-z]+)')[0]
df['House with'] = df['Description'].str.extract(r'have ([A-Za-z]+)')[0]
df

 Price  Location    House with  Description
0   90    Brazil    swimming    This house in Brazil cost $90 and       have swimming pool
1   50 Argentina    Garden      his house in Argentina cost $50 and        have Garden
2   70     Chile    Garden      This house in Chile cost $70 and have Garden
3   80      EEuu    swimming    This house in EEuu cost $80 and        have swimming pool

Или

df['Price'] = df['Description'].str.extract(r'\$(\d+)',expand=False)
df['Location'] = df['Description'].str.extract(r'house in ([A-Za-z]+)',expand=False)
df['House with'] = df['Description'].str.extract(r'have ([A-Za-z]+)',,expand=False)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...