RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / user-388715

Anofelesoff's questions

Martin Hope
Anofelesoff
Asked: 2022-10-02 05:27:17 +0000 UTC

如何将对象转换为列的时间格式?

  • 1

您需要将 'object' 10:22:55 格式的字符串转换为 'time' 格式。

我以这种格式尝试过:

data['start_time'] = datetime.time.strptime(data['start_time']).isoformat()
python
  • 1 个回答
  • 10 Views
Martin Hope
Anofelesoff
Asked: 2020-06-09 05:15:53 +0000 UTC

如何以“年”格式将浮点数据类型转换为日期?

  • 2

有一个 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)
python
  • 1 个回答
  • 10 Views
Martin Hope
Anofelesoff
Asked: 2020-06-03 16:31:27 +0000 UTC

从两个 DataFrame 计算指标

  • 0

有必要按月计算每个用户在每种服务(消息和电话)上花费的金额以及每月花费的资金总额)有关资费限制的信息在 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

在此处输入图像描述

python
  • 2 个回答
  • 10 Views
Martin Hope
Anofelesoff
Asked: 2020-05-26 18:59:56 +0000 UTC

如何在 Matplotlib 中的函数中组织数据标签

  • 0

需要为每个图形制作标签并更改图例中的标签


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

拟形成的时间表

pandas
  • 1 个回答
  • 10 Views
Martin Hope
Anofelesoff
Asked: 2020-05-25 04:39:15 +0000 UTC

如何在 Matplotlib 中创建一个循环以使用声明的参数进行自动绘图

  • 1

有一个功能

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()

但所有数据都出现在一张图表上。因此,您应该为列获得三个不同的图表

python
  • 2 个回答
  • 10 Views
Martin Hope
Anofelesoff
Asked: 2020-05-22 15:54:34 +0000 UTC

如何在 Matplotlib 中创建一个函数以使用声明的参数进行自动绘图

  • 2

有必要自动构建一系列图表,其中只有一列发生变化。

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'

python
  • 1 个回答
  • 10 Views
Martin Hope
Anofelesoff
Asked: 2020-05-22 04:34:36 +0000 UTC

使用平均值从 DataFrame 创建字典

  • 2

我有一个包含两列的列表。有必要根据这个原则从中创建一个字典:对于房间的每个值,确定该区域的平均值。

我是通过搜索每种房间类型的平均值来实现的,但是代码量很大,需要修改。

该列表应如下所示:

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
python
  • 1 个回答
  • 10 Views
Martin Hope
Anofelesoff
Asked: 2020-05-17 22:52:38 +0000 UTC

按条件替换现有 DataFrame 中的数据

  • 2

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。

有没有更简单的方法可行?

python
  • 1 个回答
  • 10 Views
Martin Hope
Anofelesoff
Asked: 2020-05-15 01:48:26 +0000 UTC

替换 Pandas Dataframe 中缺失的日期

  • 1

有一个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]"
python
  • 2 个回答
  • 10 Views

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5