python AttributeError中的类型转换:'str'对象没有属性'astype'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41917379/
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
Type Conversion in python AttributeError: 'str' object has no attribute 'astype'
提问by ftxx
I am confused by the type conversion in python pandas
我对 python pandas 中的类型转换感到困惑
df = pd.DataFrame({'a':['1.23', '0.123']})
type(df['a'])
df['a'].astype(float)
Here df
is a pandas series and its contents are 2 strings, then I can apply astype(float)
on this pandas series, and it correctly convert all string into float. However
这df
是一个pandas系列,它的内容是2个字符串,然后我可以申请astype(float)
这个pandas系列,它正确地将所有字符串转换为浮点数。然而
df['a'][1].astype(float)
gives me AttributeError: 'str' object has no attribute 'astype'. My question is: how can that be? I could convert the whole series from string to float but I couldn't convert the entry of this series from string to float?
给我 AttributeError: 'str' object has no attribute 'astype'。我的问题是:怎么可能?我可以将整个系列从字符串转换为浮点数,但我无法将这个系列的条目从字符串转换为浮点数?
Also, I load my raw data set
另外,我加载了我的原始数据集
df['id'].astype(int)
it generates ValueError: invalid literal for int() with base 10: ''
This one seems to suggest that there is a blank in my df['id']
. So I check whether it is true by typing
它生成 ValueError: invalid literal for int() with base 10: '' 这似乎表明我的df['id']
. 所以我通过输入来检查它是否是真的
'' in df['id']
it says false. So I am very confused.
它说是假的。所以我很困惑。
回答by Boud
df['a']
returns a Series
object that has astype
as a vectorized way to convert all elements in the series into another one.
df['a']
返回一个Series
对象,该对象具有astype
将系列中的所有元素转换为另一个元素的矢量化方式。
df['a'][1]
returns the content of one cell of the dataframe, in this case the string '0.123'
. This is now returning a str
object that doesn't have this function. To convert it use regular python instruction:
df['a'][1]
返回数据帧的一个单元格的内容,在本例中为 string '0.123'
。现在返回一个str
没有这个函数的对象。要转换它,请使用常规的 python 指令:
type(df['a'][1])
Out[25]: str
float(df['a'][1])
Out[26]: 0.123
type(float(df['a'][1]))
Out[27]: float
As per your second question, the operator in
that is at the end calling __contains__
against the series with ''
as argument, here is the docstring of the operator:
根据您的第二个问题,最后的运算符以作为参数in
调用__contains__
系列''
,这是运算符的文档字符串:
help(pd.Series.__contains__)
Help on function __contains__ in module pandas.core.generic:
__contains__(self, key)
True if the key is in the info axis
It means that the in
operator is searching your empty string in the index, not the contents of it.
这意味着in
操作员正在索引中搜索您的空字符串,而不是它的内容。
The way to search your empty strings is to use the equal operator:
搜索空字符串的方法是使用相等运算符:
df
Out[54]:
a
0 42
1
'' in df
Out[55]: False
df==''
Out[56]:
a
0 False
1 True
df[df['a']=='']
Out[57]:
a
1
回答by Ricardo Busquet
df['a'][1]
will return the actual value inside the array, at the position 1
, which is in fact a string. You can convert it by using float(df['a'][1])
.
df['a'][1]
将返回数组内的实际值,在位置1
,实际上是一个字符串。您可以使用float(df['a'][1])
.
>>> df = pd.DataFrame({'a':['1.23', '0.123']})
>>> type(df['a'])
<class 'pandas.core.series.Series'>
>>> df['a'].astype(float)
0 1.230
1 0.123
Name: a, dtype: float64
>>> type(df['a'][1])
<type 'str'>
For the second question, maybe you have an empty value on your raw data. The correct test would be:
对于第二个问题,也许您的原始数据有一个空值。正确的测试应该是:
>>> df = pd.DataFrame({'a':['1', '']})
>>> '' in df['a'].values
True
Source for the second question: https://stackoverflow.com/a/21320011/5335508
第二个问题的来源:https: //stackoverflow.com/a/21320011/5335508
回答by kamran kausar
data1 = {'age': [1,1,2, np.nan],
'gender': ['m', 'f', 'm', np.nan],
'salary': [2,1,2, np.nan]}
x = pd.DataFrame(data1)
for i in list(x.columns):
print(type((x[i].iloc[1])))
if isinstance(x[i].iloc[1], str):
print("It is String")
else:
print('Not a String')