python dataframe 新增一列 list
时间: 2023-09-08 07:11:09 浏览: 148
在Python的DataFrame中新增一列list可以通过以下几种方法实现:
1. 直接在DataFrame后面新增一列,指明列名,并赋值即可:
data\['addlist'\] = \[1, 2\] \[1\]
2. 在指定位置新增一列,可以使用insert()函数,语法为:data.insert(位置, 列名, 列值)。例如:
data.insert(2, 'c', '') \[3\]
3. 根据现有列计算生成新的列,可以使用apply()函数和lambda表达式。例如:
df2\['newColumn'\] = df2.apply(lambda x: \[x\['column1'\], x\['column2'\]\], axis=1) \[3\]
希望以上方法能够帮助到您。
#### 引用[.reference_title]
- *1* [Python在Dataframe中新添加一列](https://blog.csdn.net/zx1245773445/article/details/99445332)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* [【20210914】【Python】Python在DataFrame中新增一列](https://blog.csdn.net/weixin_40583722/article/details/120281217)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
- *3* [python dataframe新增一列](https://blog.csdn.net/julyclj55555/article/details/122450287)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
阅读全文