У меня есть очищенный набор данных в моем созданном фрейме данных, и сейчас я нахожусь в процессе изменения типов данных столбцов в нем.Я пытался использовать метод astype, однако при его использовании я получаю сообщение об ошибке
Второй вопрос: как преобразовать типы данных моего столбца в пригодные для использования типы данных, которые я могу использовать при визуализации данных?
Я использовал метод astype безуспешно
#Import data into a dataframe
raw_data = pd.read_csv('FuelPrices2016 -2019 ulsp.csv')
raw_data.head()
Date Pump price in pence/litre ULSP Duty rate in pence/litre/ULSP VAT percentage rate Unnamed: 4
0 02/01/2012 132.40 57.95 20 NaN
1 09/01/2012 132.68 57.95 20 NaN
2 16/01/2012 133.29 57.95 20 NaN
3 23/01/2012 133.72 57.95 20 NaN
4 30/01/2012 134.10 57.95 20 NaN
b
#Drop unnamed column
raw_b = raw_data.drop(columns=['Unnamed: 4',])
raw_b
Date Pump price in pence/litre ULSP Duty rate in pence/litre/ULSP VAT percentage rate
0 02/01/2012 132.40 57.95 20
1 09/01/2012 132.68 57.95 20
2 16/01/2012 133.29 57.95 20
3 23/01/2012 133.72 57.95 20
4 30/01/2012 134.10 57.95 20
... ... ... ... ...
396 05/08/2019 128.37 57.95 20
397 12/08/2019 128.36 57.95 20
398 19/08/2019 128.17 57.95 20
399 26/08/2019 128.22 57.95 20
400 02/09/2019 127.86 57.95 20
401 rows × 4 columns
#Describe the data
raw_b.describe()
Pump price in pence/litre ULSP Duty rate in pence/litre/ULSP VAT percentage rate
count 401.000000 4.010000e+02 401.0
mean 123.043840 5.795000e+01 20.0
std 10.175522 7.114304e-15 0.0
min 101.360000 5.795000e+01 20.0
25% 115.600000 5.795000e+01 20.0
50% 123.270000 5.795000e+01 20.0
75% 130.830000 5.795000e+01 20.0
max 142.170000 5.795000e+01 20.0
#Check the types of the columns
raw_b.dtypes
Date object
Pump price in pence/litre ULSP float64
Duty rate in pence/litre/ULSP float64
VAT percentage rate int64
dtype: object
c
#Change date into a string
raw_c = raw_b.astype({'Date': str})
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-20-64e4aadc3ce7> in <module>
----> 1 raw_d = raw_c.astype({'Date': str})
~\Anaconda3\envs\py3-TF2.0\lib\site-packages\pandas\core\generic.py in astype(self, dtype, copy, errors, **kwargs)
5855 if col_name not in self:
5856 raise KeyError(
-> 5857 "Only a column name can be used for the "
5858 "key in a dtype mappings argument."
5859 )
KeyError: 'Only a column name can be used for the key in a dtype mappings argument.'
Я ожидаю, что кадр данных изменится на строку, но выводится это сообщение об ошибке
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-20-64e4aadc3ce7> in <module>
----> 1 raw_d = raw_c.astype({'Date': str})
~\Anaconda3\envs\py3-TF2.0\lib\site-packages\pandas\core\generic.py in astype(self, dtype, copy, errors, **kwargs)
5855 if col_name not in self:
5856 raise KeyError(
-> 5857 "Only a column name can be used for the "
5858 "key in a dtype mappings argument."
5859 )
KeyError: 'Only a column name can be used for the key in a dtype mappings argument.'