How do I remove name and dtype from pandas output

Question:

I have output that looks like this:

nutrition_info_256174499 = df1.loc[:"Salt" , "%Reference Intake*"]

print (nutrition_info_256174499)

Typical Values
Energy                5%
Fat                   1%
of which saturates    1%
Carbohydrates         7%
of which sugars       2%
Fibre                  -
Protein               7%
Salt                  6%
Name: %Reference Intake*, dtype: object

What must be done to remove both Name and dtype at end of output?

Asked By: Nabih

||

Answers:

Use the .values attribute.

Example:

In [158]: s = pd.Series(['race','gender'], index=[1,2])

In [159]: print(s)
1      race
2    gender
dtype: object

In [160]: s.values
Out[160]: array(['race', 'gender'], dtype=object)

You can convert to a list or access each value:

In [161]: list(s)
Out[161]: ['race', 'gender']
Answered By: seralouk

For printing with preserving index you can use Series.to_string():

df = pd.DataFrame(
    {'a': [1, 2, 3], 'b': [2.23, 0.23, 2.3]},
    index=['x1', 'x2', 'x3'])
s = df.loc[:'x2', 'b']    
print(s.to_string())

Output:

x1    2.23
x2    0.23
Answered By: Darkonaut
print(nutrition_info_256174499.to_string())

that should remove Name: %Reference Intake*, dtype: object in your prints

Answered By: warkitty

Simply converting the dataframe values to a list with .tolist() removes the dtype element. After you can just loop through the list to get the single values:

df_as_a_list = df.values.tolist()
Answered By: ThomasAFink

Maybe this isn’t exactly what you want, but for what it’s worth, if you put the column name in a list, you get a DataFrame out instead of a Series, with the name at the top instead of bottom and no dtype.

df1.loc[:"Salt", ["%Reference Intake*"]]
                   %Reference Intake*
Typical Values                       
Energy                             5%
Fat                                1%
of which saturates                 1%
Carbohydrates                      7%
of which sugars                    2%
Fibre                               -
Protein                            7%
Salt                               6%
Answered By: wjandrea
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.