假设三个 csv 文件名为 file1.csv、file2.csv 和 file3.csv,文件中的内容格式相同,可以按照列拼接在一起
时间: 2023-11-20 19:07:28 浏览: 83
可以使用 pandas 库来实现csv文件的拼接操作。具体步骤如下:
1. 导入 pandas 库
``` python
import pandas as pd
```
2. 读取三个 csv 文件并存储为 DataFrame 对象
``` python
df1 = pd.read_csv('file1.csv')
df2 = pd.read_csv('file2.csv')
df3 = pd.read_csv('file3.csv')
```
3. 使用 pandas 库的 concat 函数将三个 DataFrame 对象按照列拼接在一起
``` python
df_concat = pd.concat([df1, df2, df3], axis=1)
```
其中,axis=1 表示按照列拼接。
4. 将合并后的 DataFrame 写入新的 csv 文件
``` python
df_concat.to_csv('merged.csv', index=False)
```
其中,index=False 表示不将索引写入文件中。
完整代码如下:
``` python
import pandas as pd
df1 = pd.read_csv('file1.csv')
df2 = pd.read_csv('file2.csv')
df3 = pd.read_csv('file3.csv')
df_concat = pd.concat([df1, df2, df3], axis=1)
df_concat.to_csv('merged.csv', index=False)
```
阅读全文
相关推荐


















