如何在Python DataFrame中拆分单元格合并数据至对应列?
Fixing DataFrame Cleaning Issues & SettingWithCopyWarning
Let's break down what's going wrong and fix it step by step:
1. Why Your Current Code Isn't Working
- Professional Missing Spaces: When you split the extracted string with
str.split("\s", expand=True), you're grabbingdate_err[0](Jenn) anddate_err[1](Blossoms) and concatenating them directly—no space in between, hence "JennBlossoms" instead of "Jenn Blossoms". - Empty/Incomplete Description: You're only taking
date_err[2](which is "Telephone"), but the full description includes everything after "Jenn Blossoms", not just a single word. - SettingWithCopyWarning: This pops up because your
date_erris created from a slice of the original DataFrame (dftopdata[s]["Date"]), and pandas is warning you that you might be modifying a copy instead of the original data.
2. Corrected Solution Code
import pandas as pd # Recreate your sample DataFrame for reference data = { "Date": [ "2019-12-19 00:00:00", "2019-12-20 00:00:00", "2019-12-27 00:00:00", "2019-12-27 00:00:00", "2019-12-30 00:00:00", "12-30-2019 Jenn Blossoms Telephone Call to A. Bell return her multiple voicemails." ], "Professional": ["Katie Cool", "Jenn Blossoms", "Jenn Blossoms", "Jenn Blossoms", "Jenn Blossoms", pd.NA], "Description": ["Travel to Space ...", "Review stuff; prepare cancellations of ...", "Review lots of stuff/o...", "Draft email to world leader...", "Review this thing.", pd.NA] } dftopdata = pd.DataFrame(data) # Step 1: Identify rows where Date parsing fails s = pd.to_datetime(dftopdata['Date'], errors='coerce').isna() # Step 2: Extract components from malformed Date strings with precise regex # Captures 3 groups: date, full professional name, full description pattern = r'^(\d{2}-\d{2}-\d{4})\s+(\w+\s\w+)\s+(.*)$' extracted = dftopdata.loc[s, 'Date'].str.extract(pattern) # Step 3: Update original DataFrame using .loc to avoid warnings dftopdata.loc[s, 'Date'] = extracted[0] dftopdata.loc[s, 'Professional'] = extracted[1] dftopdata.loc[s, 'Description'] = extracted[2] # Verify the fixed result print(dftopdata)
3. Key Improvements Explained
- Precise Regex: The pattern
r'^(\d{2}-\d{2}-\d{4})\s+(\w+\s\w+)\s+(.*)$'captures exactly what we need in one go:- Group 1: The standalone date (
12-30-2019) - Group 2: The full professional name (with space included:
Jenn Blossoms) - Group 3: The complete description text after the name
- Group 1: The standalone date (
- Eliminated Copy Warnings: By using
dftopdata.loc[s, 'Date']directly for extraction, we ensure we're modifying the original DataFrame, not a slice copy. - Full Description Capture: Group 3 grabs every part of the text after the professional name, so your description will show the full message instead of a single word.
4. Handling Edge Cases
If you have professional names with more than two words (e.g., "Mary Ann Smith"), adjust the regex pattern to capture multi-word names: use (\w+(?:\s\w+)+) instead of (\w+\s\w+) to match one or more word groups separated by spaces.
内容的提问来源于stack exchange,提问作者Harrison Alley
相关产品推荐
相关产品推荐

