您需要将 'object' 10:22:55 格式的字符串转换为 'time' 格式。
我以这种格式尝试过:
data['start_time'] = datetime.time.strptime(data['start_time']).isoformat()
您需要将 'object' 10:22:55 格式的字符串转换为 'time' 格式。
我以这种格式尝试过:
data['start_time'] = datetime.time.strptime(data['start_time']).isoformat()
有一个 DataFrame,其中年份表示为浮点数据类型。它必须转换为年份格式的日期数据类型,例如 - 2006-01-01。
0 2006.0
1 1985.0
2 2008.0
3 2009.0
4 1996.0
尝试过:
data['year_of_release'] = pd.to_datetime(data['year_of_release'], format='%d.%m.%Y %H:%M:%S')
出现以下错误:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/pandas/core/tools/datetimes.py in _convert_listlike_datetimes(arg, format, name, tz, unit, errors, infer_datetime_format, dayfirst, yearfirst, exact)
431 try:
--> 432 values, tz = conversion.datetime_to_datetime64(arg)
433 return DatetimeIndex._simple_new(values, name=name, tz=tz)
pandas/_libs/tslibs/conversion.pyx in pandas._libs.tslibs.conversion.datetime_to_datetime64()
TypeError: Unrecognized value type: <class 'int'>
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
4 frames
/usr/local/lib/python3.6/dist-packages/pandas/core/tools/datetimes.py in _convert_listlike_datetimes(arg, format, name, tz, unit, errors, infer_datetime_format, dayfirst, yearfirst, exact)
398 try:
399 result, timezones = array_strptime(
--> 400 arg, format, exact=exact, errors=errors
401 )
402 if "%Z" in format or "%z" in format:
pandas/_libs/tslibs/strptime.pyx in pandas._libs.tslibs.strptime.array_strptime()
ValueError: time data '2006' does not match format '%d.%m.%Y %H:%M:%S' (match)
有必要按月计算每个用户在每种服务(消息和电话)上花费的金额以及每月花费的资金总额)有关资费限制的信息在 df1 中提供,有关信息df2中的用户自己。如果消息或流量超过资费,那么我们考虑用户为此付出了多少,如果他没有超过限制,那么我们认为成本为0。
df1 = [[50, 4000, 3, 5, tarif1], [1000, 2000, 2, 4, tarif2]]
columns = ['messages_included', 'minutes_included','rub_per_message', 'rub_per_minute', 'tariff_name']
df2 = [[1, tarif1, 5, 20, 100], [1, tarif1, 6, 150, 250], [1, tarif1, 7, 100, 200], [2, tarif1, 5, 45, 10], [2, tarif1, 6, 100, 0], [3, tarif2, 5, 12, 98], [3, tarif2, 6, 10, 6000], [3, tarif2, 7, 800, 2000], [4, tarif2, 5, 80, 10], [4, tarif2, 6, 1000, 500]
columns = ['user_id', 'tariff', 'month', 'total_messages', 'calls_sum']
预期结果,列将添加到 df2,即每月呼叫和消息的成本,以及总成本超过资费的列,包括消息和呼叫的成本。
columns = ['user_id', 'tariff', 'month', 'total_messages', 'calls_sum', 'messages_revenue', 'calls_revenue', 'total_revenue']
决赛桌应如下所示https://imgur.com/a/yvL3mgA
需要为每个图形制作标签并更改图例中的标签
def morb_mort_plot(data, x, y, ylim, figsize=(12, 5), grid=True):
data.plot(figsize=figsize, grid=grid, x=x, y = y)
plt.legend(bbox_to_anchor=(1.2, 0.6))
morb_mort_plot(data = data.query('mkb == "C00_C96"' ), x = 'year', y = ['crude_rates', 'asr'], ylim = [0, 100])
morb_mort_plot(data = data.query('mkb == "C33_C34"' ), x = 'year', y = ['crude_rates', 'asr'], ylim = [0, 100])
morb_mort_plot(data = data.query('mkb == "C50"' ), x = 'year', y = ['crude_rates', 'asr'], ylim = [0, 100])
morb_mort_plot(data = data.query('mkb == "C53"' ), x = 'year', y = ['crude_rates', 'asr'], ylim = [0, 100])
当你试图把标签不履行的功能。尝试设置 xlabel 和 ylable 重命名时相同
示例数据框
mkb year number crude_rates asr
C34 2015 85 2 1,5
C34 2018 19 1,61 0,98
C34 2019 27 2,29 1,48
C34 2017 44 3,73 2,54
C34 2015 35 1,19 0,73
有一个功能
def super_show_spb(data, index, values, locality_name, columns=None, aggfunc="count",
fill_value=0, dropna=True, figsize=(12, 5), grid=True, **kwargs):
(city_center_data
.query('locality_name == locality_name')
.pivot_table(index=index, values=values, columns=columns,
aggfunc=aggfunc, fill_value=fill_value, dropna=dropna)
.plot.bar(figsize=figsize, grid=grid, **kwargs))
super_show_spb(data= city_center_data, index='year', values='last_price', locality_name = '"Санкт-Петербург" and cityCenters_nearest_km == 3')
我怎么能用循环做同样的事情
for columns in city_center_data(['year', 'month', 'weekday']):
(city_center_data.query('locality_name == "Санкт-Петербург" and cityCenters_nearest_km == 3')
.pivot_table(index=column, values= 'last_price', aggfunc='count', fill_value=0)
.plot.bar(figsize=(12, 5), grid=True))
plt.show()
以格式尝试过
for columns in city_centre_data:
columns = (['year', 'month', 'weekday'])
(city_center_data.query('locality_name == "Санкт-Петербург" and cityCenters_nearest_km == 3')
.pivot_table(index=columns, values= 'last_price', aggfunc='count', fill_value=0)
.plot.bar(figsize=(12, 5), grid=True))
plt.show()
但所有数据都出现在一张图表上。因此,您应该为列获得三个不同的图表
有必要自动构建一系列图表,其中只有一列发生变化。
month_last_price = data.pivot_table(index='month',
values = 'last_price',
aggfunc = 'count').plot(grid=True, figsize=(12, 5))
day_last_price = data.pivot_table(index='weekday',
values = 'last_price',
aggfunc = 'count').plot(grid=True, figsize=(12, 5))
通过函数或循环执行此操作的最佳方法是什么?像这样试过
def super_show(index_data, value_data):
super_show.plot(index = index_data, values = value_data, grid=True, figsize=(12, 5))
super_show('year', 'last_price')
plt.show()
但写道
AttributeError:'function'对象没有属性'plot'
我有一个包含两列的列表。有必要根据这个原则从中创建一个字典:对于房间的每个值,确定该区域的平均值。
我是通过搜索每种房间类型的平均值来实现的,但是代码量很大,需要修改。
该列表应如下所示:
dict_rooms = {
0: 18.0,
1: 17.4,
2: 30.0}
日期框架示例
rooms area
1 16
0 20
3 21
0 24
0 4
2 32
4 45
2 50
5 60
5 30
5 20
3 240
2 33
2 20
1 110
1 23
DataFrame 有一个从 0 到 19 的房间列表。您需要匹配它。
从DataFrame中移除所有room值\u003d 0的数据,并包含所有rooms>4的数据,替换为4。
尝试了几种方法:
data["rooms"] = data["rooms"].drop(np.where(data['rooms'] == 0)[0])
data["rooms"] = data[data["rooms"] >= 4 ].replace((data["rooms"] >= 4), 4)
第二:
rooms_data = data.drop(np.where(data['rooms'] == 0)[0])
def rooms_data_norma(row):
if row['rooms'] >= 4:
return row['rooms'] == 4
else:
return row['rooms']
结果,可以摆脱 0 值,但不可能用同一 DataFrame 中的必要值替换所有大于该值的值。
必需:只有行应该保留在最初没有 0 的 DataFrame 中,并且大于 4 的所有内容都应该替换为 4。
有没有更简单的方法可行?
有一个DataFrame,其中有“事件开始日期”(日期),“从事件日期到结束日期的天数”(int64),其中缺少值。任务是填写缺失值,假设所有缺失值的结束日期相同。
尝试以不同的方式进行,但在工作过程中不断出现错误。包括在计算结束日期 - 开始日期时,转换为的结果天数int
给出了太大的值。
也收到此错误“[Int64Index([ 0, 81, 558, 424, 121, 55, 155, 0, 189, 289,\n ...\n 29, 15, 519, 413, 239, 0, 45, 0, 602, 0],\n dtype='int64', length=23699)] 在 [columns]"
data['day_exposition_status'] = data['days_exposition'].isna()
data['days_exposition'].fillna(0, inplace=True)
data['days_exposition'] = data['days_exposition'].astype('int')
zero_days_exposition = datetime.datetime(2020, 4, 27) - data['first_day_exposition']
data['days_exposition'] = data[data['days_exposition']].replace(to_replace = 0, value = zero_days_exposition)
数据框示例 - https://pastebin.com/vWeyedxv
Полностью текст ошибки
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-24-cccce5c1358b> in <module>
1 data['days_exposition'] = data['days_exposition'].astype('int')
2 zero_days_exposition = datetime.datetime(2020, 4, 27) - data['first_day_exposition']
----> 3 data['days_exposition'] = data[data['days_exposition']].replace(to_replace = 0, value = zero_days_exposition)
/opt/conda/lib/python3.7/site-packages/pandas/core/frame.py in __getitem__(self, key)
2984 if is_iterator(key):
2985 key = list(key)
-> 2986 indexer = self.loc._convert_to_indexer(key, axis=1, raise_missing=True)
2987
2988 # take() does not accept boolean indexers
/opt/conda/lib/python3.7/site-packages/pandas/core/indexing.py in _convert_to_indexer(self, obj, axis, is_setter, raise_missing)
1283 # When setting, missing keys are not allowed, even with .loc:
1284 kwargs = {"raise_missing": True if is_setter else raise_missing}
-> 1285 return self._get_listlike_indexer(obj, axis, **kwargs)[1]
1286 else:
1287 try:
/opt/conda/lib/python3.7/site-packages/pandas/core/indexing.py in _get_listlike_indexer(self, key, axis, raise_missing)
1090
1091 self._validate_read_indexer(
-> 1092 keyarr, indexer, o._get_axis_number(axis), raise_missing=raise_missing
1093 )
1094 return keyarr, indexer
/opt/conda/lib/python3.7/site-packages/pandas/core/indexing.py in _validate_read_indexer(self, key, indexer, axis, raise_missing)
1175 raise KeyError(
1176 "None of [{key}] are in the [{axis}]".format(
-> 1177 key=key, axis=self.obj._get_axis_name(axis)
1178 )
1179 )
KeyError: "None of [Int64Index([ 0, 81, 558, 424, 121, 55, 155, 0, 189, 289,\n ...\n 29, 15, 519, 413, 239, 0, 45, 0, 602, 0],\n dtype='int64', length=23699)] are in the [columns]"