python dataframe 横向合并
时间: 2023-10-22 14:24:28 浏览: 343
python pandas dataframe 按列或者按行合并的方法
5星 · 资源好评率100%
要在Python中横向合并DataFrame,可以使用pandas库的concat方法。具体操作步骤如下:
1. 首先导入必要的库:
```python
import pandas as pd
```
2. 读取要合并的DataFrame数据:
```python
df1 = pd.read_excel('文件路径1.xlsx', sheet_name='工作表1')
df2 = pd.read_excel('文件路径2.xlsx', sheet_name='工作表2')
```
3. 使用concat方法进行横向合并:
```python
df_combined = pd.concat([df1, df2], axis=1)
```
其中,参数axis=1表示横向合并。
4. 打印合并后的结果:
```python
print(df_combined)
```
以上是使用concat方法进行DataFrame横向合并的步骤和代码示例。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *3* [Pandas统计分析基础(5):DataFrame的合并(内含大量代码可供练习)](https://blog.csdn.net/l1796333514/article/details/123651411)[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^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *2* [Python——把几个DataFrame合并成一个DataFrame](https://blog.csdn.net/J__aries/article/details/128924927)[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^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文