Ich habe eine große CSV-Datei, die ich Spalten spalten möchte. Das heißt, einige angegebene Spalten kommen in einen Teil, andere in einen anderen Teil. Ich möchte auch mehr als 2 Teile erstellen können. Wie kann ich das in Python machen? Gibt es auch eine Bibliothek in Python, um viele Datenformate zu verarbeiten?Wie teilt man CSV in Teile mit Spaltennamen?
Eingangsformat:
policyID statecode county eq_site_limit hu_site_limit fl_site_limit fr_site_limit tiv_2011 tiv_2012 eq_site_deductible hu_site_deductible fl_site_deductible fr_site_deductible point_latitude point_longitude line construction point_granularity
119736 FL CLAY COUNTY 498960 498960 498960 498960 498960 792148.9 0 9979.2 0 0 30.102261 -81.711777 Residential Masonry 1
448094 FL CLAY COUNTY 1322376.3 1322376.3 1322376.3 1322376.3 1322376.3 1438163.57 0 0 0 0 30.063936 -81.707664 Residential Masonry 3
206893 FL CLAY COUNTY 190724.4 190724.4 190724.4 190724.4 190724.4 192476.78 0 0 0 0 30.089579 -81.700455 Residential Wood 1
333743 FL CLAY COUNTY 0 79520.76 0 0 79520.76 86854.48 0 0 0 0 30.063236 -81.707703 Residential Wood 3
172534 FL CLAY COUNTY 0 254281.5 0 254281.5 254281.5 246144.49 0 0 0 0 30.060614 -81.702675 Residential Wood 1
Eingangsformat Spalten:
policyID statecode county eq_site_limit hu_site_limit fl_site_limit fr_site_limit tiv_2011 tiv_2012 eq_site_deductible hu_site_deductible fl_site_deductible fr_site_deductible point_latitude point_longitude line construction point_granularity
Ausgabeformat Spalten:
Part A: ['policyID', 'statecode', 'county', 'eq_site_limit', 'hu_site_limit']
Teil B: ['fl_site_limit', 'fr_site_limit', 'tiv_2011', 'tiv_2012', 'eq_site_deductible', 'hu_site_deductible', 'fl_site_deductible', 'fr_site_deductible', 'point_latitude', 'point_longitude', 'line', 'construction', 'point_granularity']
Code:
import csv
import pandas as pd
df = pd.read_csv("FL_insurance_sample.csv")
cl_list = list(df.columns.values)
a = cl_list[:5]
b = cl_list[5:]
with open('data1.csv', 'w') as datafile:
for x in a:
saved_column = df[x]
datafile.write(saved_column)
with open('data2.csv', 'w') as datafile:
for x in b:
saved_column = df[x]
datafile.write(saved_column)
Wir müssten ein Beispiel der csv-Datei und des Codes, den Sie geschrieben haben, sehen, um sie zu verarbeiten. –
Auch ein Hinweis auf das Format, das der Ausgang haben muss. –
Was bedeutet "viele Datenformate"? – DyZ