如何使用 pandas 将数据从csv加载到numpy数组中 [英] How to load data from csv into numpy array using pandas
本文介绍了如何使用 pandas 将数据从csv加载到numpy数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用pandas库将csv行加载到numpy数组中.我可以使用pandas读取csv,但还没有找到任何允许在csv文件中逐行读取的功能.
I want to load csv rows into a numpy array using pandas library. I can read the csv using pandas but havent found any function that allows reading row by row in csv file.
如何使用熊猫从csv逐行读取并将其放入数组?
How do I read row by row from csv using pandas and put it into an array?
例如
Parch Ticket Fare Cabin Embarked
0 0 A/5 21171 7.2500 NaN S
1 0 PC 17599 71.2833 C85 C
我想创建一个这样的列表列表:
I want to create a list of lists like this:
[
[0, "A/5", 21171, 7.2500, NaN, "S"],
[0, "PC", 17599, 71.2833, "C85", "C"]
]
推荐答案
首先由values
,然后
First convert to numpy array
by values
and then ndarray.tolist
:
print (df.values)
[[0 'A/5 21171' 7.25 nan 'S']
[0 'PC 17599' 71.2833 'C85' 'C']]
print (df.values.tolist())
[[0, 'A/5 21171', 7.25, nan, 'S'], [0, 'PC 17599', 71.2833, 'C85', 'C']]
这篇关于如何使用 pandas 将数据从csv加载到numpy数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文