python - 更改数据框数据类型以明确使用该数据进行数据可视化
问题描述
我有一个数据框,其中包含以下类型的列:object、float 64 和 int64。我想将这些类型更改为可用于数据可视化的类型。我已经尝试过使用 astype 方法,但出现错误。如何将这些类型更改为更适合数据可视化的类型。
#Import data into a dataframe
raw_data = pd.read_csv('FuelPrices2016 -2019 ulsp.csv')
raw_data.head()
#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
#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
#Change date into a date
raw_b(pd.to_datetime(['Date']))
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
~\Anaconda3\envs\py3-TF2.0\lib\site-packages\pandas\core\arrays\datetimes.py in objects_to_datetime64ns(data, dayfirst, yearfirst, utc, errors, require_iso8601, allow_object)
1978 try:
-> 1979 values, tz_parsed = conversion.datetime_to_datetime64(data)
1980 # If tzaware, these values represent unix timestamps, so we
pandas\_libs\tslibs\conversion.pyx in pandas._libs.tslibs.conversion.datetime_to_datetime64()
TypeError: Unrecognized value type: <class 'str'>
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
<ipython-input-22-5ab1ccfbf9a4> in <module>
1 #Change date into a date
----> 2 raw_b(pd.to_datetime(['Date']))
~\Anaconda3\envs\py3-TF2.0\lib\site-packages\pandas\util\_decorators.py in wrapper(*args, **kwargs)
206 else:
207 kwargs[new_arg_name] = new_arg_value
--> 208 return func(*args, **kwargs)
209
210 return wrapper
~\Anaconda3\envs\py3-TF2.0\lib\site-packages\pandas\core\tools\datetimes.py in to_datetime(arg, errors, dayfirst, yearfirst, utc, box, format, exact, unit, infer_datetime_format, origin, cache)
792 result = _convert_and_box_cache(arg, cache_array, box)
793 else:
--> 794 result = convert_listlike(arg, box, format)
795 else:
796 result = convert_listlike(np.array([arg]), box, format)[0]
~\Anaconda3\envs\py3-TF2.0\lib\site-packages\pandas\core\tools\datetimes.py in _convert_listlike_datetimes(arg, box, format, name, tz, unit, errors, infer_datetime_format, dayfirst, yearfirst, exact)
461 errors=errors,
462 require_iso8601=require_iso8601,
--> 463 allow_object=True,
464 )
465
~\Anaconda3\envs\py3-TF2.0\lib\site-packages\pandas\core\arrays\datetimes.py in objects_to_datetime64ns(data, dayfirst, yearfirst, utc, errors, require_iso8601, allow_object)
1982 return values.view("i8"), tz_parsed
1983 except (ValueError, TypeError):
-> 1984 raise e
1985
1986 if tz_parsed is not None:
~\Anaconda3\envs\py3-TF2.0\lib\site-packages\pandas\core\arrays\datetimes.py in objects_to_datetime64ns(data, dayfirst, yearfirst, utc, errors, require_iso8601, allow_object)
1973 dayfirst=dayfirst,
1974 yearfirst=yearfirst,
-> 1975 require_iso8601=require_iso8601,
1976 )
1977 except ValueError as e:
pandas\_libs\tslib.pyx in pandas._libs.tslib.array_to_datetime()
pandas\_libs\tslib.pyx in pandas._libs.tslib.array_to_datetime()
pandas\_libs\tslib.pyx in pandas._libs.tslib.array_to_datetime_object()
pandas\_libs\tslib.pyx in pandas._libs.tslib.array_to_datetime_object()
pandas\_libs\tslibs\parsing.pyx in pandas._libs.tslibs.parsing.parse_datetime_string()
~\Anaconda3\envs\py3-TF2.0\lib\site-packages\dateutil\parser\_parser.py in parse(timestr, parserinfo, **kwargs)
1356 return parser(parserinfo).parse(timestr, **kwargs)
1357 else:
-> 1358 return DEFAULTPARSER.parse(timestr, **kwargs)
1359
1360
~\Anaconda3\envs\py3-TF2.0\lib\site-packages\dateutil\parser\_parser.py in parse(self, timestr, default, ignoretz, tzinfos, **kwargs)
647
648 if res is None:
--> 649 raise ValueError("Unknown string format:", timestr)
650
651 if len(res) == 0:
ValueError: ('Unknown string format:', 'Date')
解决方案
对象的格式是正确的,您可以尝试使用以下方法将“日期”列设置为日期时间类型:
raw_b['Date'] = pd.to_datetime(raw_b['Date'])
如果这不起作用,您应该使用以下方法传递格式:
raw_b['Date'] = pd.to_datetime(raw_b['Date'], format= '%d/%m/%Y')
然后使用这些数据绘制图表。您的下一步是将Date
as x 轴传递给您的绘图,或者将其设置为数据框中的索引(因为当日期在索引中时,某些函数更容易应用):
raw_b = raw_b.set_index('Date')
这是代码:
import pandas as pd
import matplotlib.pyplot as plt
a = {'Date':['02/01/2012','09/01/2012','16/01/2012','23/01/2012','30/01/2012'],'Pump price in pence/litre ULSP':[132.40,132.68,133.29,133.72,134.10],'Duty rate in pence/litre/ULSP':[57.95,57.95,57.95,57.95,57.95],'VAT percentage rate':[20,20,20,20,20]}
raw_b = pd.DataFrame(a)
print(raw_b)
print(raw_b.dtypes)
raw_b['Date'] = pd.to_datetime(raw_b['Date'],format='%d/%m/%Y')
print(raw_b.dtypes)
print(raw_b)
raw_b = raw_b.set_index('Date')
print(raw_b)
raw_b.plot.line()
plt.show()
这是第一个打印(它最初的样子)以及类型:
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
Date object
Pump price in pence/litre ULSP float64
Duty rate in pence/litre/ULSP float64
VAT percentage rate int64
dtype: object
应用pd.to_datetime
并传递正确格式后:
Date datetime64[ns]
Pump price in pence/litre ULSP float64
Duty rate in pence/litre/ULSP float64
VAT percentage rate int64
dtype: object
这就是它的样子:
Date Pump price in pence/litre ULSP Duty rate in pence/litre/ULSP VAT percentage rate
0 2012-01-02 132.40 57.95 20
1 2012-01-09 132.68 57.95 20
2 2012-01-16 133.29 57.95 20
3 2012-01-23 133.72 57.95 20
4 2012-01-30 134.10 57.95 20
最后,设置Date
为索引后:
Pump price in pence/litre ULSP Duty rate in pence/litre/ULSP VAT percentage rate
Date
2012-01-02 132.40 57.95 20
2012-01-09 132.68 57.95 20
2012-01-16 133.29 57.95 20
2012-01-23 133.72 57.95 20
2012-01-30 134.10 57.95 20
推荐阅读
- kubernetes - 将日志文件写入慢速磁盘或将 Tomcat 访问日志发送到 ElasticSearch?
- python - 给定pandas DataFrame中一列中的值列表,如何从同一行中的另一列输出值?
- javascript - 如何将前六个月放入数组
- python - 为什么此函数会引发“无效语法”错误?
- c# - 如何有效地在大量代码库中搜索多个文字字符串?
- node.js - 为什么使用不同版本的 NPM?
- mysql - 在 MySQL 中,一个单词的查询也会匹配带有特殊字符的单词
- android - React Navigation - Auth Flow - 错误指定的孩子在Android上已经有一个父母
- javascript - 我有一个动画,我想在它显示后执行
- powerbi - 编写 DAX 以返回特定日期和日期间隔内这些日期的总数