Pandas 的 .to_string(index=True) 不遵循文档
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27711001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pandas's .to_string(index=True) not following documentation
提问by Anton
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.
如果另一列的值符合某些条件,我想打印一列的值。在下面的示例中,如果学生的分数为 5,我想打印他们的姓名。(为简单起见,数据框中只有一个学生。
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:
当我尝试使用 .to_string 函数时,我仍然得到索引号:
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() documentationand found this:
最后,我查看了 .to_string()文档,发现了这个:
index : bool, optional whether to print index (row) labels, default True
index : bool,可选是否打印索引(行)标签,默认为True
However, when I try to use .to_string(index=False), I get an error:
但是,当我尝试使用 .to_string(index=False) 时,出现错误:
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'
回答by BrenBarn
The documentation you linked to is for DataFrame.to_string, but you are calling to_stringon a Series (since you start by selecting one column with df['name']). The documentationfor Series.to_stringshows that it doesn't accept an indexargument.
您链接到的文档适用于DataFrame.to_string,但您正在调用to_string一个系列(因为您首先选择带有 的一列df['name'])。 该文档的Series.to_string显示,它不接受的index说法。
One could argue that maybe it should have one, but the behavior is at least consistent with the documentation.
有人可能会争辩说,也许它应该有一个,但行为至少与文档一致。
回答by Zenadix
The expression:
表达方式:
df['name'][df['score'] == 5][0]
returns a Seriesof 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.
返回一个系列的字符串。如果你想打印Series中的所有字符串,你应该像迭代列表或元组一样迭代它。
Therefore, if you want to print the names of all the students whose score is 5:
因此,如果要打印所有分数为 5 的学生的姓名:
for student in df['name'][df['score'] == 5]:
print(student)
回答by ragesz
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

