简体   繁体   English

Python:如何在遍历熊猫数据框时获取值而不是对象类型?

[英]Python: How to get values and not object type while iterating through pandas Data Frame?

I am trying to fetch values of each row of pandas dataframe using below code. 我正在尝试使用以下代码获取熊猫数据帧的每一行的值。

points = [ makePoint(row) for row in df.iterrows() ]

here df has 4 column. 此处df有4列。 each contains integer data. 每个都包含整数数据。

while i try to print row, it returns follow, 当我尝试打印行时,它返回跟随,

1    3
2    3
3    4
4    5
Name: 0, dtype: int64

I just need 我只需要

[3,3,4,5] 

and don't want index, name and dtype. 并且不需要索引,名称和dtype。

Use tolist() if need convert Series to list and iterrows return tuples - index with Series , so add i for unpacking it: 如果需要将Series转换为list并使用iterrows返回iterrows带有Series索引,请使用tolist() ,因此请添加i以对其进行解包:

points = [ makePoint(row).tolist() for i, row in df.iterrows() ]

Or list : list

points = [ list(makePoint(row)) for i, row in df.iterrows() ]

Another solution: 另一个解决方案:

points = df.apply(makePoint, axis=1).values.tolist()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM