Pandas Dataframe str.match и str.contain - PullRequest
       4

Pandas Dataframe str.match и str.contain

0 голосов
/ 21 апреля 2020

У меня есть json файл с

{
    "London": "Location A",   
    "Berlin": "Location B"  
}

У меня есть другой Dataframe с 2 столбцами

Canberra is the capital of Australia            AUS  1
Berlin is the capital of Germany                GER  1
London is the capital of United Kingdom         UK   1
Berlin is also the art capital of Germany       GER  1
There is a direct flight from berlin to london  OTH  1
Interstate train service are halted             OTH  0

Я пытаюсь запустить ключи json и выберите все строки, содержащие строку (точное совпадение), как в текущем ключе.

То, что я пробовал до сих пор:

temp_df = pd.read_csv(fileName , header=None)
df = (temp_df[temp_df[2] == 1]).reset_index(drop=True)
print(df)

with open(jsonFileName, encoding='utf-8') as jsonFile:
    jsonData = json.load(jsonFile)

for key in jsonData.keys():
    print(key)
    df2 = (df[df[0].str.lower().str.match(r"\b{}\b".format(key), case=False)]).reset_index(drop=True)
    print(df2.head())

Когда я пытаюсь использовать contains

 df2 = (df[df[0].str.lower().str.contains(r"\b{}\b".format(key), regex=True, case=False)]).reset_index(drop=True)
 print(df2.head())

Ожидаемый вывод: Для ключа = Лондон

London is the capital of United Kingdom         UK   1
There is a direct flight from berlin to london  OTH  1

Но, его выброс: удвоенные результаты

London is the capital of United Kingdom         UK   1
London is the capital of United Kingdom         UK   1
There is a direct flight from berlin to london  OTH  1
There is a direct flight from berlin to london  OTH  1

Любой указатель на это полезен.

1 Ответ

1 голос
/ 21 апреля 2020

Мне все еще не совсем ясно, что вы хотите сделать, но похоже, что вы ищете нечувствительное к регистру совпадение для ряда строк.

Вот способ сделать это с Series.str.contains.

with open(jsonFileName, encoding='utf-8') as jsonFile:
    jsonData = json.load(jsonFile)

# convert the series of strings into lower-case
haystack = df[0].str.lower()

for key in jsonData.keys():

    # convert the key to lower-case
    needle = key.lower()

    # create a boolean indexer of any records in the haystack containing the needle
    matches = haystack.str.contains(needle)

    # create a subset of the dataframe with only those rows
    df2 = df[matches]
    print(df2)

Вы также можете использовать Series.apply для дополнительной настройки:

    matches = haystack.apply(lambda x: needle in x)

Вот полный код с предоставленными примерами данных:

# setup the sample data objects
jsonData = {
    "Berlin": "Location A",
    "London": "Location B"
}

temp_df = pd.DataFrame([
    {0: 'Canberra is the capital of Australia', 1: 'AUS', 2: 1},
    {0: 'Berlin is the capital of Germany', 1: 'GER', 2: 1},
    {0: 'London is the capital of United Kingdom', 1: 'UK', 2: 1},
    {0: 'Berlin is also the art capital of Germany', 1: 'GER', 2: 1},
    {0: 'There is a direct flight from berlin to london', 1: 'OTH', 2: 1},
    {0: 'Interstate train service are halted', 1: 'OTH', 2: 0}
])

df = (temp_df[temp_df[2] == 1]).reset_index(drop=True)


# convert the series of strings into lower-case
haystack = df[0].str.lower()

for key in jsonData.keys():

    # convert the key to lower-case
    needle = key.lower()

    # create a boolean indexer of any records in the haystack containing the needle
    matches = haystack.str.contains(needle)

    # create a subset of the dataframe with only those rows
    df2 = df[matches]
    print(df2)

Выход:

                                             0    1  2
2         London is the capital of United Kingdom   UK  1
4  There is a direct flight from berlin to london  OTH  1

                                                0    1  2
1                Berlin is the capital of Germany  GER  1
3       Berlin is also the art capital of Germany  GER  1
4  There is a direct flight from berlin to london  OTH  1
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...