How to get a single value as a string from pandas data frame

前端 未结 3 1337
闹比i
闹比i 2021-02-20 04:05

I am querying a single value from my data frame which seems to be \'dtype: object\'. I simply want to print the value as it is with out printing the index or other information a

3条回答
  •  太阳男子
    2021-02-20 04:31

    it should work simply..

    >>> df
      Host Port
    0    a    b
    >>> df[df['Host'] == 'a']['Port'][0]   # will choose the first index simply which is 'b'
    'b'
    

    OR, use with print which will strip off the surrounded single ticks.

    >>> print(df[df['Host'] == 'a']['Port'][0])
    b
    

    This will easier because you have just choose the desired Index even if you have Multiple values across Port columns

    Example:

    >>> df
      Host Port
    0    a    b
    1    c    c
    

    Looking for distinct a & c based on Index:

    >>> df[df['Host'] == 'a']['Port'][0]
    'b'
    >>> df[df['Host'] == 'c']['Port'][1]
    'c'
    

提交回复
热议问题