Riješeno: dtype ne može biti bool python

Posljednje ažuriranje: 09/11/2023

Glavni problem je što je bool cjelobrojni tip, dok je dtype str tip. Ovo može uzrokovati probleme kada pokušavate uporediti dvije bool vrijednosti ili kada bacate jednu na drugu.

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>

O Dtype

Dtype je tip podataka u Pythonu koji predstavlja broj s pomičnim zarezom.

objekti

U Pythonu objekti su strukture podataka koje vam omogućavaju pohranjivanje informacija na jednoj lokaciji. Objekti se mogu kreirati iz bilo koje vrste podataka, uključujući brojeve, nizove i liste.

Objektima se može pristupiti pomoću operatora tačke ( . ), a također im se može dodijeliti promjenljivim pomoću operatora dodjeljivanja ( = ). Također možete koristiti metode objekta za izvođenje radnji na objektu.

Na primjer, možete koristiti metodu objekta print() za prikaz informacija o objektu na ekranu. Također možete koristiti metodu objekta __str__() da vratite string reprezentaciju objekta.

Slični postovi: