You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python Pandas合并两个DataFrame出现空值错误的解决方法

Solution to Merge DataFrames Without NaNs

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 renaming df2’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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 10:14:05