คลายแฟ้มข้อมูลและจัดเรียงพวกมันตามวันที่

0

คำถาม

ฉันกำลังพยายามคิดให้ออกแบบฝึกหัดในข้อความหุ้นและการเรียงลำดับ. ออกกำลังกาถามเพื่อแยกคำพูดนั้นมีเวลาอ้างอิง(e.g.,ชั่วโมง,วัน)จากข้อความและเรียงลำดับแถวจากพื้นฐานที่สะกัดออกมาใน ascendent คำสั่ง เป็นตัวอย่างของข้อมูลคือ:

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            

ในงานนี้ผมสามารถบอกได้ว่าหลายวามยากลำบาก:

- 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

ที่นี่คือจุดแรกที่ฉันรู้แล้โดยปกติแล้วคนที่คู่เดทมาก่อน—ไม่ว่าของขวัญดังนั้นมันอาจจะเป็นเรื่องง่ายที่จะคลายแฟ้มพวกเขา สองจุดนักถ้าแถลงการณ์จะสามารถหลีกเลี่ยงข้อความผิดพลาดเนื่องจากยังไม่สมบูรณ์/หายไปฟิลด์ ฉันไม่รู้ว่าจะต้องตอบจุดที่สามที่นะ

ฉันคาดหวังว่าผลจะเป็น:

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

คำตอบที่ดีที่สุด

1

จาก DataFrame ตัวอย่างฉันจะถือว่านั่นสำหรับการออกกำลังกายนี้คนแรกสองคำพูดของข้อความเป็นสิ่งที่คุณกำหลังจากนั้น ฉันยังไม่ค่อยแน่ใจว่างการเรียงลำดับงานแต่สำหรับสามจุดเป็นตามที่เหมาะสมมากกว่านี้เวลาจะเป็น current time - timedelta จากที่ข้อความอลัมน์

คุณสามารถปรับใช้เป็นการอื่นอีก lambda ฟังก์ชันที่สองคนแรกคำพูดของแต่ละแถวด้วของ Text และแปลงนี้จะเป็น pandas Timedelta วัตถุ-ตัวอย่างเช่น pd.Timedelta("1 day") จะกลับมาเป็น Timedelta เดาใจ

งั้นคุณสามารถลดค Timedelta คอลัมน์จากปัจจุบันเวลาซึ่งคุณสามารถการได้รับ 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"]

แสดงผล:

>>> 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

ในภาษาอื่นๆ

หน้านี้อยู่ในภาษาอื่นๆ

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

ดังอยู่ในนี้หมวดหมู่

ดังคำถามอยู่ในนี้หมวดหมู่