我有一个存储在csv文件中的数据,如下面的格式
892,3,"Kelly, Mr. James",male,34.5,0,0,330911,7.8292,,Q
893,3,"Wilkes, Mrs. James (Ellen Needs)",female,47,1,0,363272,7,,S
894,2,"Myles, Mr. Thomas Francis",male,62,0,0,240276,9.6875,,Q
895,3,"Wirz, Mr. Albert",male,27,0,0,315154,8.6625,,S
896,3,"Hirvonen, Mrs. Alexander (Helga E Lindqvist)",female,22,1,1,3101298,12.2875,,S
897,3,"Svensson, Mr. Johan Cervin",male,14,0,0,7538,9.225,,S
每个列的数据类型
1. int 6. int
2. int 7. int
3. String 8. float
4. String 9. float
5. float 10.String
11.String
以 892,893,... 897 开头的第一列应以int
格式存储在array
中。像“威尔克斯,詹姆斯夫人(Ellen Needs)”这样的第三栏应该存储在string
类型中。但是,第三列是string
格式,但字符的长度是不是固定,即我不知道此列中存储的字符的最大长度
我做过:
csv_file_object = csv.reader(open('trainData.csv', 'rb'))
header = csv_file_object.next()
data=[]
for row in csv_file_object:
data.append(row)
data = np.array(data)
但是,上面的代码将所有列读为string
,但其中许多不是string
格式,并将信息存储在string
格式。另一方面,如果我使用genfromtxt
,第三列是问题,因为它包含双配额内的逗号。
我希望每个列都有自己的数据类型,即第一列应该存储为int
类型。
我预期的数组:
892 3 "Kelly, Mr. James" male 34.5 0 0 330911 7.8292 NaN Q
893 3 "Wilkes, Mrs. James (Ellen Needs)" female 47 1 0 363272 7 NaN S
894 2 "Myles, Mr. Thomas Francis" male 62 0 0 240276 9.6875 NaN Q
895 3 "Wirz, Mr. Albert" male 27 0 0 315154 8.6625 NaN S
896 3 "Hirvonen, Mrs. Alexander (Helga E Lindqvist)" female 22 1 1 3101298 12.2875 NaN S
897 3 "Svensson, Mr. Johan Cervin" male 14 0 0 7538 9.225 S
如您所见,如果数据不可用,则应放置NaN
或其衍生物。
我应该阅读什么csv文件?
答案 0 :(得分:0)
我不确定我是否理解你,但我认为这对你有用。
我实现了另外两个函数,它们将决定字符串是浮点数还是整数。
如果字符串是空字符串,我写了None,但是,您可以将其更改为您喜欢的任何内容。
import csv
import numpy as np
def isfloat(x):
try:
a = float(x)
except ValueError:
return False
else:
return True
def isint(x):
try:
a = float(x)
b = int(a)
except ValueError:
return False
else:
return a == b
csv_file_object = csv.reader(open('trainData.csv', 'rb'))
header = csv_file_object
data=[]
for row in csv_file_object:
for index, cell in enumerate(row):
if isint(cell):
row[index] = int(cell)
elif isfloat(cell):
row[index] = float(cell)
if not cell: # cell == ''
row[index] = None # you can change the value to whatever you like.
data.append(row)
print data
<强>输出:强>
[[892, 3, 'Kelly, Mr. James', 'male', 34.5, 0, 0, 330911, 7.8292, None, 'Q'],
[893, 3, 'Wilkes, Mrs. James (Ellen Needs)', 'female', 47, 1, 0, 363272, 7, None, 'S'],
[894, 2, 'Myles, Mr. Thomas Francis', 'male', 62, 0, 0, 240276, 9.6875, None, 'Q'],
[895, 3, 'Wirz, Mr. Albert', 'male', 27, 0, 0, 315154, 8.6625, None, 'S'],
[896, 3, 'Hirvonen, Mrs. Alexander (Helga E Lindqvist)', 'female', 22, 1, 1, 3101298, 12.2875, None, 'S'],
[897, 3, 'Svensson, Mr. Johan Cervin', 'male', 14, 0, 0, 7538, 9.225, None, 'S']]
答案 1 :(得分:0)
您可以更轻松地使用pandas库,如下所示:
import pandas as pd
df = pd.read_csv("trainData.csv", dtype={'col1': int, 'col2': int, 'col3': str, 'col4': str, 'col5': float, 'col6':int,
'col7': int, 'col8': float, 'col9':float, 'col10': str, 'col11': str})
df = map(list, df.values)
print df
<强>输出:强>
[[892, 3, 'Kelly, Mr. James', 'male', 34.5, 0, 0, 330911.0, 7.8292, nan, 'Q'],
[893, 3, 'Wilkes, Mrs. James (Ellen Needs)', 'female', 47.0, 1, 0, 363272.0, 7.0, nan, 'S'],
[894, 2, 'Myles, Mr. Thomas Francis', 'male', 62.0, 0, 0, 240276.0, 9.6875, nan, 'Q'],
[895, 3, 'Wirz, Mr. Albert', 'male', 27.0, 0, 0, 315154.0, 8.6625, nan, 'S'],
[896, 3, 'Hirvonen, Mrs. Alexander (Helga E Lindqvist)', 'female', 22.0, 1, 1, 3101298.0, 12.2875, nan, 'S'],
[897, 3, 'Svensson, Mr. Johan Cervin', 'male', 14.0, 0, 0, 7538.0, 9.225, nan, 'S']]
csv文件应该如下所示,因为第一行是列名
col1,col2,col3,col4,col5,col6,col7,col8,col9,col10,col11
892,3,"Kelly, Mr. James",male,34.5,0,0,330911,7.8292,,Q
893,3,"Wilkes, Mrs. James (Ellen Needs)",female,47,1,0,363272,7,,S
894,2,"Myles, Mr. Thomas Francis",male,62,0,0,240276,9.6875,,Q
895,3,"Wirz, Mr. Albert",male,27,0,0,315154,8.6625,,S
896,3,"Hirvonen, Mrs. Alexander (Helga E Lindqvist)",female,22,1,1,3101298,12.2875,,S
897,3,"Svensson, Mr. Johan Cervin",male,14,0,0,7538,9.225,,S
您可以在此处阅读有关pandas的更多信息http://pandas.pydata.org/pandas-docs/stable/tutorials.html
答案 2 :(得分:0)
我假设您正在使用熊猫,因为问题是标记为pandas。像这样读取文件:
df = pd.read_csv('test.txt', skiprows=0, index_col=0,
names='city_type name sex weight has_cat has_dog bank_balance body_fat_index car_mileage car_type'.split())
您将获得如下数据框:
我冒昧地为列编制名称。
一旦你将数据读入数据框,就可以用它做各种各样的魔术 - 看看pandas教程(它们很棒)。这是一个例子
df.bank_balance.describe()
count 6.000000
mean 726408.166667
std 1170522.652019
min 7538.000000
25% 258995.500000
50% 323032.500000
75% 355181.750000
max 3101298.000000
Name: bank_balance, dtype: float64