[英]How to read data from csv file if all the values are in the same column?
我有一個csv文件,格式如下:
"age","job","marital","education","default","balance","housing","loan"
58,"management","married","tertiary","no",2143,"yes","no"
44,"technician","single","secondary","no",29,"yes","no"
但是,它們不是由制表符(不同的列)分隔,而是位於相同的第一列中。 當我嘗試使用pandas讀取它時,輸出會在同一列表中提供所有值,而不是列表列表。
我的代碼:
dataframe = pd.read_csv("marketing-data.csv", header = 0, sep= ",")
dataset = dataframe.values
print(dataset)
O / P:
[[58 'management' 'married' ..., 2143 'yes' 'no']
[44 'technician' 'single' ..., 29 'yes' 'no']]
我需要的:
[[58, 'management', 'married', ..., 2143, 'yes', 'no']
[44 ,'technician', 'single', ..., 29, 'yes', 'no']]
我錯過了什么?
我認為你對print()
輸出感到困惑,它沒有顯示逗號。
演示:
In [1]: df = pd.read_csv(filename)
熊貓代表:
In [2]: df
Out[2]:
age job marital education default balance housing loan
0 58 management married tertiary no 2143 yes no
1 44 technician single secondary no 29 yes no
Numpy代表:
In [3]: df.values
Out[3]:
array([[58, 'management', 'married', 'tertiary', 'no', 2143, 'yes', 'no'],
[44, 'technician', 'single', 'secondary', 'no', 29, 'yes', 'no']], dtype=object)
Numpy string
表示( print(numpy_array)
):
In [4]: print(df.values)
[[58 'management' 'married' 'tertiary' 'no' 2143 'yes' 'no']
[44 'technician' 'single' 'secondary' 'no' 29 'yes' 'no']]
結論:您的CSV文件已正確解析。
我真的沒有看到你想要的和你得到的東西之間的區別......但是使用內置的csv模塊解析csv文件可以得到你想要的結果
import csv
with open('file.csv', 'rb') as csvfile:
spamreader = csv.reader(csvfile, delimiter=',', quotechar='|')
print list(spamreader)
[
['年齡','工作','婚姻','教育','默認','平衡','住房','貸款'],
['58','管理','已婚','大專','不','2143','是','不'],
['44','技師','單身','中學','不','29','是','不']
]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.