遍历具有索引的列

时间:2019-10-07 06:43:22

标签: python pandas

我如何遍历大熊猫中具有索引的列,对于行,我们可以使用for i, j in df.iterrows():来给出索引和行。

列是否有类似内容?

spice smice skice bike dike mike 
    1     23     35    34   34   56 
    135   34     23    21   56   34
    231   12     67    21   62   75

我正在尝试使用嵌套的for循环,如下所示:

for index, col1 in  df.columns:
  for col2 in df.columns[index:]:

还有更好的方法吗?

2 个答案:

答案 0 :(得分:2)

我相信您需要按列名循环,并为Series按列名选择:

for col_name in df.columns:
    print (col_name)
    print (df[col_name])

替代解决方案,缺点是可读性较差:

for col_name in df:
    print (col_name)
    print (df[col_name])

您可以通过DataFrame.T进行转置来解决您的问题,但在我看来有点复杂:

for col_name, s in df.T.iterrows():
    print (col_name)
    print (s)

编辑:

for col_name in df.columns:
    print (col_name)
    print (df[col_name])
    print (df.columns.get_loc(col_name))

答案 1 :(得分:0)

我找到了以下解决方案,但不确定这样做是否正确,我是熊猫新手。

Imports System.Drawing.Drawing2D


    Class MyButton
        Inherits Button

        Private m_TopColor As Color = Color.LightGreen
        Private m_BottomColor As Color = Color.Orange

        Public Property TopColor As Color
            Get
                Return m_TopColor
            End Get
            Set(ByVal value As Color)
                m_TopColor = value
                Me.Invalidate()
            End Set
        End Property

        Public Property BottomColor As Color
            Get
                Return m_BottomColor
            End Get
            Set(ByVal value As Color)
                m_BottomColor = value
                Me.Invalidate()
            End Set
        End Property

        Public Sub New()
            FlatStyle = FlatStyle.Flat
            FlatAppearance.BorderSize = 0
            FlatAppearance.BorderColor = Color.FromArgb(0, 0, 0, 0)
            FlatAppearance.MouseDownBackColor = Color.Transparent
            FlatAppearance.MouseOverBackColor = Color.Transparent
            BackColor = Color.Transparent
            Me.BackgroundImageLayout = ImageLayout.Zoom

            AddHandler Me.MouseMove, AddressOf MyMouseMove
            AddHandler Me.MouseLeave, AddressOf MyMouseLeave

        End Sub
        Private Sub MyMouseLeave(ByVal sender As Object, ByVal e As EventArgs)

        End Sub

        Private Sub MyMouseMove(ByVal sender As Object, ByVal e As MouseEventArgs)

        End Sub

        Private Sub Draw(ByVal rectangle As Rectangle, ByVal g As Graphics, ByVal cusp As Boolean)

            Dim span As Integer = 2
            g.SmoothingMode = SmoothingMode.AntiAlias
            Dim myLinearGradientBrush As LinearGradientBrush = New LinearGradientBrush(Me.ClientRectangle, m_TopColor, m_BottomColor, 90.0F)


            g.FillPath(myLinearGradientBrush, DrawRoundRect(rectangle.X, rectangle.Y, rectangle.Width - span, rectangle.Height - 1, 20))
        End Sub
        Private Function DrawRoundRect(ByVal x As Integer, ByVal y As Integer, ByVal width As Integer, ByVal height As Integer, ByVal radius As Integer) As GraphicsPath
            Dim gp As GraphicsPath = New GraphicsPath()
            gp.AddArc(x, y, radius, radius, 180, 90)
            gp.AddArc(width - radius, y, radius, radius, 270, 90)
            gp.AddArc(width - radius, height - radius, radius, radius, 0, 90)
            gp.AddArc(x, height - radius, radius, radius, 90, 90)
            gp.CloseAllFigures()
            Return gp
        End Function

        Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
            MyBase.OnPaint(e)

            Draw(e.ClipRectangle, e.Graphics, False)

        End Sub

        Private Function GetVerticalAlignment() As StringAlignment
            Return CType(Math.Log(Me.TextAlign, 2D) / 4, StringAlignment)
        End Function

        Private Function GetHorizontalAlignment() As StringAlignment
            Return CType(Math.Log(Me.TextAlign, 2D) Mod 4, StringAlignment)
        End Function

    End Class