使用熊猫连接2个CSV文件,但不包含行索引

时间:2018-06-27 15:30:14

标签: python pandas csv

如何使用熊猫连接2个CSV文件,以使生成的CSV文件没有行索引?请参见下面的示例:

file1.csv

date                    type      price           amount    
2017-09-02 00:00:01     b         0.279111        94000       
2017-09-02 00:00:01     b         0.279           43800          
2017-09-02 00:00:01     b         0.2789          6000     
2017-09-02 00:00:01     b         0.278812        2000         
2017-09-02 00:00:01     b         0.278           7250   

file2.csv

date                    type    price           amount    
2018-03-02 00:00:01     q         0.279111       1400       
2018-05-01 00:00:01     q         0.279          3800          
2017-09-02 00:00:01     q         0.2789         640  

desired_file.csv

date                   type       price           amount    
2017-09-02 00:00:01     b         0.279111        94000       
2017-09-02 00:00:01     b         0.279           43800          
2017-09-02 00:00:01     b         0.2789          6000     
2017-09-02 00:00:01     b         0.278812        2000         
2017-09-02 00:00:01     b         0.278           7250     
2018-03-02 00:00:01     q         0.279111        1400       
2018-05-01 00:00:01     q         0.279           3800          
2017-09-02 00:00:01     q         0.2789          640   

不是这样的

     date                   type       price           amount    
1    2017-09-02 00:00:01     b         0.279111        94000       
2    2017-09-02 00:00:01     b         0.279           43800          
3    2017-09-02 00:00:01     b         0.2789          6000     
4    2017-09-02 00:00:01     b         0.278812        2000         
5    2017-09-02 00:00:01     b         0.278           7250     
6    2018-03-02 00:00:01     q         0.279111        1400       
7    2018-05-01 00:00:01     q         0.279           3800          
8    2017-09-02 00:00:01     q         0.2789          640   

我的代码:

import pandas as pd

a = pd.read_csv("file1.csv")
b = pd.read_csv("file2.csv")

combined = pd.concat([a, b])

combined.to_csv("desired_file.csv")

谢谢:)

1 个答案:

答案 0 :(得分:1)

请阅读docs

combined.to_csv("desired_file.csv", index=False)