2016-05-24 29 views
1

Diese Frage mit dieser Geschichte ist: LinkPandas- Split Text in der Spalte und in Zeilen suchen

Hier ist eine JSON-Format Tabelle:

ID Title 
19 I am doing great 
25 [Must fix problem] Stomach not well 
31 [Not-so-urgent] Wash cloths 
498 [VERY URGENT] Pay your rent 
517 Landlord wants you to pay your rent tomorrow 
918 Girlfriend wants to help you to pay rent if you take her out 
1000 [Always reproducible issue] Room partner dont want to pay any rent, he is out of cash 

Ich tat dies

In: selected_row_title = df.loc [df ['id'] == 4] ["title"]

Ausgabe:

[VERY URGENT] Pay your rent 

nun von Python Pandas mit, Ich versuche, eine Funktion zu schreiben, wie:

get_matching_rows(selected_row_title) 

Ausgabe

ID 498 has pay your rent 
ID 517 has pay your rent 
ID 918 has pay rent 
ID 1000 has pay rent 

Ich habe auf diese meine Haare wurden zu reißen, und ich brauche wirklich einige Hilfe, zumindest eine Anleitung, wie dies einmal umgesetzt werden kann. Schätzen Sie alle Eingaben.

+1

Habe ich Recht davon ausgehen, dass Sie alle Zeilen auswählen möchten, die „pay rent“ in Spalte „Titel“ haben? Die Beschreibung scheint mir etwas unklar zu sein. –

+0

Ja, wählen Sie alle Zeilen aus, die in 'title' die Option 'Miete mieten' enthalten. danke für Ihre Hilfe. –

Antwort

1

Ich glaube, Sie str.replace mit str.contains verwenden:

s = "[VERY URGENT] Pay your rent" 

#replace all [] in column title 
tit = df.Title.str.replace(r'[\[\]]', '') 
print (tit) 

0          I am doing great 
1     Must fix problem Stomach not well 
2       Not-so-urgent Wash cloths 
3       VERY URGENT Pay your rent 
4   Landlord wants you to pay your rent tomorrow 
5 Girlfriend wants to help you to pay rent if yo... 
6 Always reproducible issue Room partner dont wa... 
Name: Title, dtype: object 

#search one of word of string s (logical or is |) 
mask = tit.str.contains(s.replace(' ', '|')) 
print (mask) 
0 False 
1 False 
2  True 
3  True 
4  True 
5  True 
6  True 
Name: Title, dtype: bool 
#select all ID by condition 
selected_row_title = df.loc[mask, 'ID'] 
print (selected_row_title) 
2  31 
3  498 
4  517 
5  918 
6 1000 
Name: ID, dtype: int64 
Verwandte Themen