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

Python中合并列名与数据类型不同的241列DataFrame

Got it, let's tackle this step by step. You've got two DataFrames with the same number of columns (241) but different setups—one has proper column names plus a single row of metadata, the other has no headers and loads of data rows. Here's how to merge them correctly:

Step 1: Align the column names

Since both DataFrames have exactly 241 columns, we can directly take the meaningful column labels from your first DataFrame (FILEID to B0_235) and assign them to the second one. This ensures columns line up perfectly for merging:

import pandas as pd

# Replace df1 and df2 with your actual DataFrame names
df2.columns = df1.columns

Step 2: Concatenate the DataFrames

Now that both share matching column names, use pd.concat() to append all rows from the second DataFrame to the first. We'll set ignore_index=True to reset the combined index, avoiding duplicate index values across the original datasets:

combined_df = pd.concat([df1, df2], axis=0, ignore_index=True)

Quick Note on Data Types

You might notice some columns shift data types after merging (e.g., an integer column from df2 becomes object type). This is pandas upcasting types to accommodate all values across both DataFrames. If you need to enforce specific types later, you can use astype() for individual columns:

# Example: Force a column to integer type (only if all values are compatible)
combined_df['FILEID'] = combined_df['FILEID'].astype(int)

Verify the Result

Double-check the combined DataFrame to confirm everything worked as expected:

print(combined_df.info())

You should see a total of 1 + 11718 = 11719 rows, with columns still labeled from FILEID to B0_235.

内容的提问来源于stack exchange,提问作者Sanjay K

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 10:52:45