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'
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With