Gelöst: dtype kann nicht bool python sein

Letzte Aktualisierung: 09/11/2023

Das Hauptproblem besteht darin, dass bool ein ganzzahliger Typ ist, während dtype ein str-Typ ist. Dies kann zu Problemen führen, wenn versucht wird, zwei boolesche Werte zu vergleichen oder einen in den anderen umzuwandeln.

I am trying to read a csv file in Python using Pandas. I have a column with values "true" and "false". When I try to read the file, I get the following error: 
<code>ValueError: could not convert string to float: 'true'
</code>
I tried changing the data type of the column to bool but it gives me an error saying that dtype cannot be bool. How can I fix this?


A:

You can use <code>pd.read_csv()</code> with <code>dtype={'column_name':bool}</code>: 
<blockquote>
<p><strong><a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_csv.html" rel="nofollow noreferrer">pd.read_csv()</a></strong></p>
<pre><code>&lt;code&gt;dtype : Type name or dict of column -&amp;gt; type, default None  

    Data type for data or columns. E.g. {‘a’: np.float64, ‘b’: np.int32}  

    Use str or object together with suitable na_values settings to preserve and not interpret dtype as numeric type for non-numeric values such as empty strings “” or “NA”:  

    df = pd . read_csv (StringIO ("""A|B|C  

        0|1|2  

        3|4|5"""), dtype=str)  

    For non-standard datetime parsing, use ``pd . date _ parse(format)`` as a converter for ``datetime`` columns or ``pd . Timestamp`` if you want more control over specifying a format specification (e . g . European style MM / DD / YYYY) which overrides the default one , specified by ``dateparse . DEFAULTS ['dateparser']`` : ` ` ` python from dateutil import parser from io import StringIO df = pd . read_csv (StringIO ("""date | time | value 08 / 01 / 2012 | 00 : 00 : 05 | 1 08 / 01 / 2012 | 00 : 00 : 06 | 2 """), parse_dates = [[0 , 1]], date _ parser = parser . parse ) # specify dayfirst=True when parsing European dates # Or for timestamps, use pd . Timestamp instead of datetime and specify format explicitly ` ` ` See also pandas-dev#12169 where it was proposed that partial string indexing be supported on datetime columns when using a converter other than ``datetime`` , e . g :: df = pd . read _ csv (StringIO ("""date time value 08 / 01 / 2012 00 : 00 : 05 1 08 / 01 / 2012 00 : 00 : 06 2 """), parse _ dates = [[0 , 1]], date _ parser = lambda x , y : pd . Timestamp ( f '{x} {y}' )) df [ 'time' ] # results in TypeError currently &lt;/code&gt;</pre></blockquote>
</blockquote>

Über Dtype

Dtype ist ein Datentyp in Python, der eine Gleitkommazahl darstellt.

Objekte

In Python sind Objekte Datenstrukturen, mit denen Sie Informationen an einem einzigen Ort speichern können. Objekte können aus jedem Datentyp erstellt werden, einschließlich Zahlen, Zeichenfolgen und Listen.

Auf Objekte kann mit dem Punktoperator ( . ) zugegriffen werden, und sie können auch mit dem Zuweisungsoperator ( = ) Variablen zugewiesen werden. Sie können auch die Methoden des Objekts verwenden, um Aktionen für das Objekt auszuführen.

Beispielsweise können Sie die Methode print() des Objekts verwenden, um Informationen über das Objekt auf dem Bildschirm anzuzeigen. Sie können auch die Methode __str__() des Objekts verwenden, um eine Zeichenfolgendarstellung des Objekts zurückzugeben.

Zusammenhängende Posts: