pandas 当列中有字符串时,如何从熊猫列中获取最长长度的字符串/整数/浮点数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34757703/
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
How to get the longest length string/integer/float from a pandas column when there are strings in the column
提问by Calamari
I have a dataframe with many columns of data and different types. I have encountered one column that has String and Integers contained within it. I am trying to find the values with the longest/shortest length (note not largest value). (NOTE: The eg I am using below only has integers in it because I couldn't work out how to mix dtypes and still call this an int64 column)
我有一个包含多列数据和不同类型的数据框。我遇到过一列,其中包含字符串和整数。我试图找到最长/最短长度的值(注意不是最大值)。(注意:我在下面使用的 eg 只有整数,因为我无法弄清楚如何混合 dtypes 并且仍然称其为 int64 列)
Name MixedField
a david 32252
b andrew 4023
c calvin 25
d david 2
e calvin 522
f david 35
The method I am using is to convert the df column to a String Series (because they might be double/int/string/combinations), and then I can get the max/min length items from this series:
我使用的方法是将 df 列转换为字符串系列(因为它们可能是 double/int/string/combinations),然后我可以从这个系列中获取最大/最小长度项:
df['MixedField'].apply(str).map(len).max()
df['MixedField'].apply(str).map(len).min()
But can't work out how to select the actual values that are the maximum and minimum length!?! (ie 32252 (longest) and 2 (shortest)
但是不知道如何选择最大和最小长度的实际值!?!(即32252(最长)和2(最短)
(I possibly don't need to explain this, but there is a subtle difference between largest and longest...(ie "aa" is longer than "z")). Appreciate your help. Thanks.
(我可能不需要解释这一点,但最大和最长之间存在细微差别......(即“aa”比“z”长))。感谢你的帮助。谢谢。
采纳答案by Happy001
I think this should work if you df
have unique indices.
我认为如果您df
有唯一的索引,这应该有效。
field_length = df.MixedField.astype(str).map(len)
print df.loc[field_length.argmax(), 'MixedField']
print df.loc[field_length.argmin(), 'MixedField']