How to add pandas data to an existing csv file?

后端 未结 6 1413
离开以前
离开以前 2020-11-22 10:02

I want to know if it is possible to use the pandas to_csv() function to add a dataframe to an existing csv file. The csv file has the same structure as the load

6条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-22 10:20

    Initially starting with a pyspark dataframes - I got type conversion errors (when converting to pandas df's and then appending to csv) given the schema/column types in my pyspark dataframes

    Solved the problem by forcing all columns in each df to be of type string and then appending this to csv as follows:

    with open('testAppend.csv', 'a') as f:
        df2.toPandas().astype(str).to_csv(f, header=False)
    

提交回复
热议问题