👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!
Why does Pandas tell me that I have objects, although every item in the selected column is a string — even after explicit conversion.
This is my DataFrame:
<class "pandas.core.frame.DataFrame">
Int64Index: 56992 entries, 0 to 56991
Data columns (total 7 columns):
id 56992 non-null values
attr1 56992 non-null values
attr2 56992 non-null values
attr3 56992 non-null values
attr4 56992 non-null values
attr5 56992 non-null values
attr6 56992 non-null values
dtypes: int64(2), object(5)
Five of them are dtype object
. I explicitly convert those objects to strings:
for c in df.columns:
if df[c].dtype == object:
print "convert ", df[c].name, " to string"
df[c] = df[c].astype(str)
Then, df["attr2"]
still has dtype object
, although type(df["attr2"].ix[0]
reveals str
, which is correct.
Pandas distinguishes between int64
and float64
and object
. What is the logic behind it when there is no dtype str
? Why is a str
covered by object
?
👻 Read also: what is the best laptop for engineering students?
We hope this article has helped you to resolve the problem. Apart from Strings in a DataFrame, but dtype is object, check other ast Python module-related topics.
Want to excel in Python? See our review of the best Python online courses 2023. If you are interested in Data Science, check also how to learn programming in R.
By the way, this material is also available in other languages:
- Italiano Strings in a DataFrame, but dtype is object
- Deutsch Strings in a DataFrame, but dtype is object
- Français Strings in a DataFrame, but dtype is object
- Español Strings in a DataFrame, but dtype is object
- Türk Strings in a DataFrame, but dtype is object
- Русский Strings in a DataFrame, but dtype is object
- Português Strings in a DataFrame, but dtype is object
- Polski Strings in a DataFrame, but dtype is object
- Nederlandse Strings in a DataFrame, but dtype is object
- 中文 Strings in a DataFrame, but dtype is object
- 한국어 Strings in a DataFrame, but dtype is object
- 日本語 Strings in a DataFrame, but dtype is object
- हिन्दी Strings in a DataFrame, but dtype is object
Berlin | 2023-01-31
I was preparing for my coding interview, thanks for clarifying this - Strings in a DataFrame, but dtype is object in Python is not the simplest one. Checked yesterday, it works!
Rome | 2023-01-31
Maybe there are another answers? What Strings in a DataFrame, but dtype is object exactly means?. I just hope that will not emerge anymore
Texas | 2023-01-31
Maybe there are another answers? What Strings in a DataFrame, but dtype is object exactly means?. Will get back tomorrow with feedback