Open CSV file and writing each row to new, dynamically named CSV file

后端 未结 3 1693
灰色年华
灰色年华 2021-01-23 19:08

I have a csv file with, say, 50 rows of data, and I would like to split it into separate csv files for each row, which includes first row (header) and the the relevant row.

3条回答
  •  执念已碎
    2021-01-23 19:24

    Here is a solution with pandas. Assume the content of csv as follows:

    Name, Age, Gender
    John, 20, Male
    Jack, 22, Male
    Jill, 18, Female
    

    And my code is as follows:

    import pandas as pd
    df = pd.read_csv("mock_data.csv")
    
    for index, row in df.iterrows():
        file_name = row['Name']+".csv"  #Change the column name accordingly
        pd.DataFrame(row).T.to_csv(file_name, index=None)
    

    This will create filenames based on the values of the column "Name" (i.e. Jack, John and Jill) to produce three files John.csv, Jack.csv and Jill.csv. Content of John.csv is as follows:

    Name    | Age   |  Gender |
    ---------------------------
    John    | 20    |  Male   |
    

    Content of Jack.csv is as follows:

    Name    | Age   |  Gender |
    ---------------------------
    Jack    | 22    |  Male   |
    

    Content of Jill.csv is as follows:

    Name    | Age   |  Gender |
    ---------------------------
    Jill    | 20    |  Female   |
    

    P.S: If you don't want the header, just add header = None when calling .to_csv() function. For example:

    pd.DataFrame(row).T.to_csv(file_name, index=None, Header=None)
    

提交回复
热议问题