I am confused by the type conversion in 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['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?
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
'' in df['id']
it says false. So I am very confused.
Kenil Vasani
df['a']
returns aSeries
object that hasastype
as a vectorized way to convert all elements in the series into another one.df['a'][1]
returns the content of one cell of the dataframe, in this case the string'0.123'
. This is now returning astr
object that doesn’t have this function. To convert it use regular python instruction: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:It means that the
in
operator is searching your empty string in the index, not the contents of it.The way to search your empty strings is to use the equal operator: