python csv文件合并按主键
时间: 2023-08-10 16:05:10 浏览: 367
python读写csv文件方法详细总结
在Python中,可以使用pandas库来合并CSV文件按主键。以下是三种常见的方法:
方法一:使用merge函数
可以使用merge函数来合并CSV文件,通过指定主键列来进行合并。示例代码如下:
```python
import pandas as pd
df1 = pd.read_csv('file1.csv')
df2 = pd.read_csv('file2.csv')
df3 = pd.read_csv('file3.csv')
merged_df = pd.merge(df1, df2, on='id', how='inner')
merged_df = pd.merge(merged_df, df3, on='id', how='inner')
merged_df.to_csv('merged_file.csv', index=False)
```
方法二:使用concat函数和groupby函数
可以使用concat函数将多个DataFrame按行合并,然后使用groupby函数按主键列进行分组和求和。示例代码如下:
```python
import pandas as pd
file_list = \['file1.csv', 'file2.csv', 'file3.csv'\]
df_list = \[\]
for file in file_list:
df = pd.read_csv(file)
df_list.append(df)
merged_df = pd.concat(df_list, axis=0, ignore_index=True).groupby('id').sum()
merged_df.to_csv('merged_file.csv')
```
方法三:使用glob库和to_csv函数
可以使用glob库来获取所有的CSV文件路径,然后使用to_csv函数将每个文件逐个合并到一个结果文件中。示例代码如下:
```python
import glob
import os
import pandas as pd
inputfile = os.path.join(os.getcwd(), 'Data', '*.csv')
outputfile = os.path.join(os.getcwd(), 'Data', 'result.csv')
csv_list = glob.glob(inputfile)
df_list = \[\]
for filepath in csv_list:
df = pd.read_csv(filepath)
df_list.append(df)
merged_df = pd.concat(df_list, axis=0, ignore_index=True)
merged_df.to_csv(outputfile, index=False)
```
以上是三种常见的方法,根据具体需求选择适合的方法来合并CSV文件按主键。
#### 引用[.reference_title]
- *1* [Python根据相同列合并csv文件(不去除不相同数据行)](https://blog.csdn.net/Han_torch/article/details/121229800)[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^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* [python根据id 合并多个csv文件](https://blog.csdn.net/m0_67268069/article/details/129785023)[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^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
- *3* [Python合并(拼接)多个CSV文件](https://blog.csdn.net/qq_40987844/article/details/101289520)[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^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
阅读全文