2017-09-06 520 views
0

我有一個很大的csv數據文件,我想使用列進行分割。也就是說,一些指定的列進入一個部分,其他一些列進入另一部分。我也希望能夠創建2個以上的零件。我如何在Python中做到這一點?另外,是否有一個python庫來處理多種數據格式?如何使用列名稱將csv拆分爲多個部分?

輸入格式:

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 

輸入格式色譜柱:

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 

輸出格式色譜柱:

部分A:['policyID', 'statecode', 'county', 'eq_site_limit', 'hu_site_limit']

部分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']

代碼:

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) 
+1

我們需要查看csv文件的示例以及您嘗試處理它的代碼。 –

+0

還有一些指示輸出必須具有的格式。 –

+0

「許多數據格式」是什麼意思? – DyZ

回答

2

我假設你想將原始數據框中的特定列分割爲新的數據框,然後分割成csv
讓我知道這個假設是否不正確,因爲答案是基於此。

OK,所以你讀csv到大熊貓數據幀(DF)

import csv 
import pandas as pd 

df = pd.read_csv("FL_insurance_sample.csv") 

然後,創建一個新的DF根據您的需求(同時在這裏你的A部分)

>>> part_A = df.filter(['policyID', 'statecode', 'county', 'eq_site_limit', 'hu_site_limit'], axis=1) 

>>> part_A 
    policyID statecode  county eq_site_limit hu_site_limit 
0  NaN  NaN   NaN   NaN   NaN 
1 119736.0  FL CLAY COUNTY  498960.0  498960.00 
2 448094.0  FL CLAY COUNTY  1322376.3  1322376.30 
3 206893.0  FL CLAY COUNTY  190724.4  190724.40 
4 333743.0  FL CLAY COUNTY   0.0  79520.76 
5 172534.0  FL CLAY COUNTY   0.0  254281.50 

發送part_A DF數據爲CSV

>>> part_A.to_csv("part_A.csv", index=False, encoding='utf-8') 

同樣創造了新的DF爲part_B

>>> part_B = df.filter(['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'], axis=1) 

然後發送part_B df到csv。

>>> part_B.to_csv("part_B.csv", index=False, encoding='utf-8') 

因此,您可以根據您的需要拆分列併發送到csv

1

寫專欄的任何列表到CSV文件中,使用功能to_csv()

df = pd.read_csv("FL_insurance_sample.csv") 

df.iloc[:,:5].to_csv("data1.csv") 
df.iloc[:,5:].to_csv("data2.csv") 

如果您想直接通過列的列表:

df[a].to_csv("data1.csv") 
df[b].to_csv("data2.csv") 
+0

如果我想提到列名而不是':5',我該怎麼做? –

+0

查看最新的答案。 – DyZ