如何在Pandas的DataFrame中的行上进行迭代? - How to iterate over rows in a DataFrame in Pandas?

08/07 23:03
阅读数 36

问题:

I have a DataFrame from pandas: 我有一个来自熊猫的DataFrame

import pandas as pd
inp = [{'c1':10, 'c2':100}, {'c1':11,'c2':110}, {'c1':12,'c2':120}]
df = pd.DataFrame(inp)
print df

Output: 输出:

   c1   c2
0  10  100
1  11  110
2  12  120

Now I want to iterate over the rows of this frame. 现在,我要遍历该框架的行。 For every row I want to be able to access its elements (values in cells) by the name of the columns. 对于每一行,我希望能够通过列名访问其元素(单元格中的值)。 For example: 例如:

for row in df.rows:
   print row['c1'], row['c2']

Is it possible to do that in pandas? 熊猫有可能这样做吗?

I found this similar question . 我发现了类似的问题 But it does not give me the answer I need. 但这并不能给我我所需的答案。 For example, it is suggested there to use: 例如,建议在那里使用:

for date, row in df.T.iteritems():

or 要么

for row in df.iterrows():

But I do not understand what the row object is and how I can work with it. 但是我不明白什么是row对象以及如何使用它。


解决方案:

参考一: https://stackoom.com/question/178Oq/如何在Pandas的DataFrame中的行上进行迭代
参考二: https://oldbug.net/q/178Oq/How-to-iterate-over-rows-in-a-DataFrame-in-Pandas
展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
在线直播报名
返回顶部
顶部