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

Python如何将向量绑定到DataFrame并保留其名称为列名?(对比R)

Best Practices to Add a New Column to a Pandas DataFrame (Similar to R's cbind())

Great question! In Python's pandas library, replicating R's cbind() behavior (adding a new column to an existing DataFrame) is straightforward, with a few flexible methods depending on your needs. Let's use your provided example to walk through each approach:

First, let's set up the initial data as you described:

import pandas as pd

web_stats = {'Day':[1,2,3,4,5,6], 'Visitors':[43,34,65,56,29,76], 'Bounce Rate':[65,67,78,65,45,52]}
df = pd.DataFrame(web_stats, columns = ['Day', 'Visitors', 'Bounce Rate'])
new_column = [2,4,6,8,10,12]

1. Direct Column Assignment (Simplest & Most Common)

This is the pandas equivalent of R's cbind() for most everyday cases. You can directly assign the new list/array to a new column name in your DataFrame. This modifies the original DataFrame in-place:

# Add the new column directly
df['new_column'] = new_column

# View the updated DataFrame
print(df)

Output:

Day  Visitors  Bounce Rate  new_column
0    1        43           65           2
1    2        34           67           4
2    3        65           78           6
3    4        56           65           8
4    5        29           45          10
5    6        76           52          12

2. Using df.assign() (Returns a New DataFrame)

If you want to avoid modifying the original DataFrame (perfect for method chaining or immutable workflows), use the assign() method. It creates and returns a new DataFrame with the added column, leaving your original data untouched:

# Create a new DataFrame with the added column
df_new = df.assign(new_column=new_column)

# Original df stays unchanged
print("Original DataFrame:")
print(df)
print("\nNew DataFrame with added column:")
print(df_new)

3. Using df.insert() (Control Column Position)

If you need to specify exactly where the new column should be inserted (not just at the end), use insert(). For example, to add the column as the last one (same as the first method):

# Insert at the last position (index = number of existing columns)
df.insert(loc=len(df.columns), column='new_column', value=new_column)

# Or insert right after the 'Visitors' column (index=2)
# df.insert(loc=2, column='new_column', value=new_column)

Key Notes:

  • Always ensure the length of new_column matches the number of rows in your DataFrame (pandas will throw an error if they don't align).
  • Direct assignment is preferred for most cases due to its simplicity and readability.
  • Use assign() when you want to keep the original DataFrame intact or chain operations (e.g., df.assign(new_col=...).filter(items=['Day', 'new_col'])).
  • Use insert() only when you need precise control over the column's position in the DataFrame.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 10:00:46