Warning: session_start(): open(/tmp/sess_7e17e51efe043c4443c07d236a81dd15, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How To Resolve "AttributeError: 'List' Object Has No Attribute 'Astype'" In Python - LearnShareIT

How To Resolve “AttributeError: ‘List’ Object Has No Attribute ‘Astype'” In Python

AttributeError: 'list' object has no attribute 'astype' in Python

If you encounter the AttributeError: ‘list’ object has no attribute ‘astype’ in Python, you can refer to our following article for a solution. Let’s go into detail now.

What causes the AttributeError: ‘list’ object has no attribute ‘astype’ in Python?

The astype() method casts the data type (dtype) in a pandas object. The method supports any data type supported by Numpy.

AttributeError is one of the exceptions in Python. AttributeError occurs when you access an undefined property on an object.

The AttributeError: ‘list’ object has no attribute ‘astype’ happens because you call the astype() method on the list. 

Example: 

myList = [1.1, 1.2, 1.3, 1.4]
print(myList.astype(str))

Output:

Traceback (most recent call last):
  File "/prog.py", line 2, in <module>
     print(myList.astype(str))
AttributeError: 'list' object has no attribute 'astype'

How to solve this error?

Convert the list to a numpy array

Converting the list to a numpy array is your handling of the error.

Syntax:

np.array()

The np.array() function returns a numpy array (the number of dimensions you initialize).

Example:

  • Import the numpy module.
  • Create a list.
  • Convert the list to a numpy array using the np.array() function.
  • Use the astype attribute to cast numpy arrays.
import numpy as np

myList = [1.1, 1.2, 1.3, 1.4]

# Convert the list to a numpy array
npArrayObj = np.array(myList)

# Use the astype attribute to cast numpy arrays
result = npArrayObj.astype(dtype=float)

print('Result after casting:', result)  

Output:

Result after casting: [1.1 1.2 1.3 1.4]

Use the dictionary data structure type instead of the list data type

Because pandas astype() supports changing multiple data types using Dict, you can use it this way.

Syntax:

DataFrame.astype(dtype, copy=True, errors="raise")

Parameters:

  • dtype: Using a numpy.dtype or Python type passes all pandas objects that appear to have the same type.
  • copy =True: Default is True. Returns a copy when copy=True. 
  • errors : Control the exception that occurs. Use ‘raise’ to raise an exception when the input is invalid. ‘ignore’ returns the original object when an error occurs.

Example:

  • Import numpy and pandas modules.
  • Initialize a dictionary of keys and values ​​that are numpy arrays.
  • Create a dataframe from dict.
  • Use the astype attribute to cast numpy arrays.
import pandas as pd
import numpy as np

# Create DataFrame from Dictionary
myDic = {
    'array1': [1.1, 1.2, 1.3],
    'array2': [2.1, 2.2, 2.3],
    'array3': [3.1, 3.2, 3.3],
  }
     
# Create a dataframe from dict
df = pd.DataFrame(myDic)

# Use the astype attribute to cast numpy arrays
df2 = df.astype({'array1':'float','array2':'float','array3':'float'})

print(df2)

Output:

   array1  array2  array3
0   1.1     2.1     3.1
1   1.2     2.2     3.2
2   1.3     2.3     3.3

Summary

I hope you understand the AttributeError: ‘list’ object has no attribute ‘astype’ in Python and can fix it quickly.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *