Estrarre i dati e li ordina per data

0

Domanda

Sto cercando di capire un esercizio di manipolazione di stringhe e di ordinamento. L'esercizio chiede di estrarre parole che hanno il tempo di riferimento (ad esempio, ore, giorni) dal testo, e ordinare le righe in base al tempo di estrarre in un ordine ascendente. Un esempio di dati:

Customer     Text
1            12 hours ago — the customer applied for a discount
2            6 hours ago — the customer contacted the customer service
3            1 day ago — the customer reported an issue
4            1 day ago — no answer
4            2 days ago — Open issue
5            

In questo compito sono in grado di identificare diverse difficoltà:

- time reference can be expressed as hours/days/weeks
- there are null values or no reference to time
- get a time format suitable and more general, e.g., based on the current datetime

Sul primo punto, ho notato che in genere le date di prima e, se presente, in modo che possa essere facile da estrarre. Sul secondo punto, se la dichiarazione potrebbe evitare i messaggi di errore a causa di dati incompleti o mancanti campi. Non so come risposta al terzo punto, però.

Il mio risultato atteso sarebbe:

Customer     Text                                                        Sort by
1            12 hours ago — the customer applied for a discount             1
2            6 hours ago — the customer contacted the customer service      2
3            1 day ago — the customer reported an issue                     2
4            1 day ago — no answer                                          2
4            2 days ago — Open issue                                        3
5            
data-manipulation pandas python
2021-11-24 01:46:37
1

Migliore risposta

1

Dato il DataFrame esempio, si assume che per questo esercizio le prime due parole del testo sono quello che sono dopo. Io sono poco chiaro come l'ordinamento funziona, ma per il terzo punto, il periodo più adatto sarebbe il current time - timedelta la colonna di Testo

È possibile applicare un if-else funzione lambda per le prime due parole di ogni riga di Text e converte un panda Timedelta oggetto - per esempio pd.Timedelta("1 day") restituirà un Timedelta oggetto.

Poi si può sottrarre l'Timedelta colonna dalla corrente del tempo che si può ottenere con pd.Timestamp.now():

df["Timedelta"] = df.Text.apply(lambda x: pd.Timedelta(' '.join(x.split(" ")[:2])) if pd.notnull(x) else x)
df["Time"] = pd.Timestamp.now() - df["Timedelta"]

Output:

>>> df
   Customer                                               Text       Timedelta                       Time
0         1  12 hours ago — the customer applied for a disc... 0 days 12:00:00 2021-11-23 09:22:40.691768
1         2  6 hours ago — the customer contacted the custo... 0 days 06:00:00 2021-11-23 15:22:40.691768
2         3         1 day ago — the customer reported an issue 1 days 00:00:00 2021-11-22 21:22:40.691768
3         4                              1 day ago — no answer 1 days 00:00:00 2021-11-22 21:22:40.691768
4         4                            2 days ago — Open issue 2 days 00:00:00 2021-11-21 21:22:40.691768
5         5                                                NaN             NaT                        NaT
2021-11-24 18:34:53

In altre lingue

Questa pagina è in altre lingue

Русский
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................