Python Pandas合并两个DataFrame出现空值错误的解决方法
The core issue here is that your second DataFrame (df2) doesn’t share the same column names as the first one (df1). When you use pd.concat without aligning columns, Pandas treats mismatched column names as separate fields, which is why you’re seeing those NaN values. Here’s how to fix this properly:
Step 1: Align df2’s column names with df1
First, rename the columns of your header-less DataFrame to match the structure of df1:
df2.columns = ['name', 'State']
This will update df2 from having columns labeled 0 and 1 to using name and State, just like df1.
Step 2: Concatenate with index reset
Now you can stack the two DataFrames row-wise, using ignore_index=True to reset the index so it runs sequentially instead of repeating 0 and 1:
combined_df = pd.concat([df1, df2], ignore_index=True)
Final Result:
name State 0 Tom NY 1 Lee CA 2 Jon FL 3 Tan NJ
Why your previous attempts didn’t work:
pd.concat([df1, df2], axis=1): This merges the DataFrames side-by-side (column-wise) instead of stacking them vertically, which isn’t what you intended.pd.concat([df1, df2], ignore_index=True): Without renamingdf2’s columns first, the column names still didn’t align—so you only fixed the index, not the column mismatch causing NaNs.
Content of the question comes from Stack Exchange, asked by Learn

