Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I remove name and dtype from pandas output

Tags:

python

pandas

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?

like image 689
Nabih Avatar asked Oct 27 '18 18:10

Nabih


People also ask

How remove unwanted data from pandas DataFrame?

Specific rows and columns can be removed from a DataFrame object using the drop() instance method. The drop method can be specified of an axis – 0 for columns and 1 for rows. Similar to axis the parameter, index can be used for specifying rows and columns can be used for specifying columns.

What does Dtypes do in pandas?

The dtypes property returns data type of each column in the DataFrame.

How do you remove an index from a data frame?

The most straightforward way to drop a Pandas dataframe index is to use the Pandas . reset_index() method. By default, the method will only reset the index, forcing values from 0 - len(df)-1 as the index. The method will also simply insert the dataframe index into a column in the dataframe.


3 Answers

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
like image 74
Darkonaut Avatar answered Nov 15 '22 06:11

Darkonaut


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']
like image 33
seralouk Avatar answered Nov 15 '22 08:11

seralouk


print(nutrition_info_256174499.to_string())

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

like image 41
warkitty Avatar answered Nov 15 '22 06:11

warkitty