取两个csv文件不相交的部分
时间: 2023-06-17 13:07:53 浏览: 90
可以使用Python的pandas库来实现这个功能。
假设我们有两个csv文件,分别是file1.csv和file2.csv,可以按照以下步骤取它们的不相交部分:
1. 使用pandas的read_csv函数读取两个csv文件,并将它们存储为两个dataframe对象,比如df1和df2。
```python
import pandas as pd
df1 = pd.read_csv('file1.csv')
df2 = pd.read_csv('file2.csv')
```
2. 使用pandas的merge函数将df1和df2合并成一个dataframe对象,使用how='outer'参数可以保留两个dataframe的所有行。
```python
merged_df = pd.merge(df1, df2, how='outer')
```
3. 使用pandas的drop_duplicates函数去除merged_df中的重复行,保留所有不相交的部分。
```python
result_df = merged_df.drop_duplicates(keep=False)
```
4. 将result_df存储为一个新的csv文件。
```python
result_df.to_csv('result.csv', index=False)
```
这样,result.csv文件就是file1.csv和file2.csv不相交的部分了。
阅读全文
相关推荐


















