dataframe载入多个文件
时间: 2024-05-26 19:07:24 浏览: 16
DataFrame是Pandas中最为常用的数据结构之一,可以方便地处理结构化数据。Pandas提供了多种方法用于读取多个文件的数据,以下是其中两种常见的方法:
1. 使用concat()函数将多个DataFrame合并成一个DataFrame
```python
import pandas as pd
# 定义文件路径列表
file_paths = ['file1.csv', 'file2.csv', 'file3.csv']
# 使用循环读取多个文件
df_list = []
for file_path in file_paths:
df = pd.read_csv(file_path)
df_list.append(df)
# 将多个DataFrame合并成一个DataFrame
df = pd.concat(df_list, axis=0, ignore_index=True)
```
2. 使用read_csv()函数读取多个文件并将它们合并成一个DataFrame
```python
import pandas as pd
# 定义文件路径列表
file_paths = ['file1.csv', 'file2.csv', 'file3.csv']
# 使用read_csv()函数读取多个文件并将它们合并成一个DataFrame
df = pd.concat([pd.read_csv(file_path) for file_path in file_paths], axis=0, ignore_index=True)
```
以上两种方法都可以实现将多个文件的数据载入到一个DataFrame中。在使用这些方法时,需要注意文件的格式和数据内容是否一致。此外,还需要注意每个文件中列名是否一致,如果不一致,则需要在合并之前先进行列名的处理。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)