Anton
Anton

Reputation: 4815

Pandas's .to_string(index=True) not following documentation

I want to print the value of one column if the value of another column matches some criteria. In the example below, I want to print the name of the student if their score is 5. (For simplicity, there is only one student in the dataframe.

df = pd.DataFrame()

df['name'] = ['jane']
df['score'] = [5]

When I try to print the name using the most simple solution, I get a bunch of stuff other than just the name:

In [62]:

print(df['name'][df['score'] == 5])
0    jane
Name: name, dtype: object

When I try to use the .to_string function, I still get the index number:

In [60]:

print(df['name'][df['score'] == 5].to_string())
Out[60]:
[0    jane
 Name: name, dtype: object]

Finally, I looked at the .to_string() documentation and found this:

index : bool, optional whether to print index (row) labels, default True

However, when I try to use .to_string(index=False), I get an error:

In [64]:

df['name'][df['score'] == 5].to_string(index = False)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-54-4ed8cfe4b098> in <module>()
----> 6 df['name'][df['score'] == 5].to_string(index = False)

TypeError: to_string() got an unexpected keyword argument 'index'

Upvotes: 3

Views: 8624

Answers (3)

ragesz
ragesz

Reputation: 9527

This would work as you prefer:

    In [10]:
    print(df[df['score'] == 5][['score','name']].to_string(columns=['name'],header=False,index=False))
    Out[10]:
     jane

Upvotes: 0

Zenadix
Zenadix

Reputation: 16279

The expression:

df['name'][df['score'] == 5][0]

returns a Series of strings. If you want to print all the strings in the Series, you should iterate it just like you would iterate a list or tuple.

Therefore, if you want to print the names of all the students whose score is 5:

for student in df['name'][df['score'] == 5]:
    print(student)

Upvotes: 2

BrenBarn
BrenBarn

Reputation: 251383

The documentation you linked to is for DataFrame.to_string, but you are calling to_string on a Series (since you start by selecting one column with df['name']). The documentation for Series.to_string shows that it doesn't accept an index argument.

One could argue that maybe it should have one, but the behavior is at least consistent with the documentation.

Upvotes: 9

Related Questions