如何合并两个DataFrame保留连接列数据?及如何合并索引数组添加零行?
Hey there! Let's break down your two Pandas questions with clear, actionable examples to solve what you're working on.
To keep all values from your join column(s) across both DataFrames, you'll want to use an outer join with pd.merge(). Unlike an inner join (which only keeps matching values), an outer join preserves every entry from the join column in either DataFrame, filling in NaN for missing values in other columns.
Example Walkthrough
Suppose you have two DataFrames sharing an id column:
import pandas as pd df1 = pd.DataFrame({'id': [1, 2, 3], 'value1': [10, 20, 30]}) df2 = pd.DataFrame({'id': [2, 3, 4], 'value2': [200, 300, 400]})
Run an outer merge to retain all id values:
merged_df = pd.merge(df1, df2, on='id', how='outer')
Result:
| id | value1 | value2 |
|---|---|---|
| 1 | 10 | NaN |
| 2 | 20 | 200 |
| 3 | 30 | 300 |
| 4 | NaN | 400 |
If you want to replace NaN with a default value (like 0), add fillna():
merged_df = merged_df.fillna(0)
Note: If your join columns have different names (e.g., id in df1 and user_id in df2), use left_on and right_on instead:
merged_df = pd.merge(df1, df2, left_on='id', right_on='user_id', how='outer')
This is super common for time series data where you need to ensure every date in your array is present, even if there's no data for it. Let's fix the issue you ran into when merging your date array.
Step-by-Step Solution
Let's use a concrete scenario matching your description:
import pandas as pd # Your existing DataFrame with some dates existing_dates = pd.date_range('2023-05-01', '2023-05-03') df = pd.DataFrame({'sales': [150, 200, 180]}, index=existing_dates) # Your full date array (includes dates missing from df) all_dates = pd.date_range('2023-05-01', '2023-05-05')
1. Create a "date skeleton" DataFrame
First, make a DataFrame that includes every date in your array (this ensures no dates get left out):
date_skeleton = pd.DataFrame(index=all_dates)
2. Merge with your original DataFrame
Use join() (or merge()) with an outer join to combine the skeleton and your df:
merged_df = date_skeleton.join(df, how='outer')
3. Fill missing values with 0
Replace any NaN (from dates with no data) with 0:
merged_df['sales'] = merged_df['sales'].fillna(0)
Final Result:
| index | sales |
|---|---|
| 2023-05-01 | 150 |
| 2023-05-02 | 200 |
| 2023-05-03 | 180 |
| 2023-05-04 | 0 |
| 2023-05-05 | 0 |
Why your initial attempt might have failed
- Date format mismatch: If your original df's dates are strings instead of
datetimeobjects, convert them first withpd.to_datetime(). - Wrong join type: Using
how='inner'instead ofhow='outer'would only keep dates present in both the df and array. - Not using a skeleton: Trying to merge directly with the array (instead of a DataFrame) can cause indexing issues.
内容的提问来源于stack exchange,提问作者Naomi Fridman

