Python 2.7下合并同结构DataFrame以减少None值
Merge Two DataFrames to Minimize None Values in Python 2.7
Since your two DataFrames share identical structure and index, the simplest and most efficient way to achieve your desired result is using pandas' combine_first() method. This method fills missing (None) values in the first DataFrame with non-missing values from the second DataFrame—exactly what you need to minimize Nones.
Step-by-Step Solution:
- Import pandas: Make sure you have pandas installed (compatible with Python 2.7—versions like pandas 0.25.x work well).
- Define your DataFrames: Recreate the sample DataFrames as provided.
- Apply
combine_first(): Use this method ondf1withdf2as the argument to get the merged result.
Code Example:
import pandas as pd # Create df1 df1 = pd.DataFrame( {'colA': ['A', None, 'B'], 'colB': [None, None, 'C'], 'colC': ['X', 'D', None]}, index=[1, 2, 3] ) # Create df2 df2 = pd.DataFrame( {'colA': [None, 'Y', None], 'colB': [None, 'Z', None], 'colC': [None, None, None]}, index=[1, 2, 3] ) # Merge to minimize None values df3 = df1.combine_first(df2) print(df3)
Output:
colA colB colC index 1 A NaN X 2 Y Z D 3 B C NaN
(Note: In pandas, None is represented as NaN for object columns, but they behave identically in this context.)
How It Works:
combine_first()prioritizes values fromdf1wherever they are not missing.- For any position where
df1has aNone(orNaN), it pulls the corresponding value fromdf2if it's non-missing. - Since your indexes are identical, the method aligns rows perfectly without needing additional merge keys.
This approach is concise, efficient, and leverages pandas' built-in functionality to handle exactly this use case.
内容的提问来源于stack exchange,提问作者Omar14
相关产品推荐
相关产品推荐

