【问题标题】:Change dimension in column dataframe python更改列数据框python中的维度
【发布时间】:2018-07-23 09:56:55
【问题描述】:

我想忽略伟大的价值观,让它们与他人处于同一水平。

我有一个像这样的数据框:

import numpy as np
import pandas as pd
ar = np.array([['2018-03-14T10:58:20.000Z', 2], 
               ['2018-03-14T11:58:20.000Z', 3],
               ['2018-03-14T12:58:20.000Z', 2],
               ['2018-03-14T13:58:20.000Z', 10],
               ['2018-03-14T14:58:20.000Z', 11],
               ['2018-03-14T15:58:20.000Z', 12],
               ['2018-03-14T16:58:20.000Z', 11],
               ['2018-03-14T17:58:20.000Z', 3],
               ['2018-03-14T18:58:20.000Z', 2],
               ['2018-03-14T19:58:20.000Z', 4],
              ])
df = pd.DataFrame(ar, columns = ['Date', 'weight'])
df['Date'] = pd.to_datetime(df['Date'])
df['weight'] = df['weight'].astype(float)

我想得到同级别的权重,比如:

df_new =      [['2018-03-14T10:58:20.000Z', 2], 
               ['2018-03-14T11:58:20.000Z', 3],
               ['2018-03-14T12:58:20.000Z', 2],
               ['2018-03-14T13:58:20.000Z', 2],
               ['2018-03-14T14:58:20.000Z', 3],
               ['2018-03-14T15:58:20.000Z', 4],
               ['2018-03-14T16:58:20.000Z', 3],
               ['2018-03-14T17:58:20.000Z', 3],
               ['2018-03-14T18:58:20.000Z', 2],
               ['2018-03-14T19:58:20.000Z', 4],
              ])

我该怎么做?

【问题讨论】:

  • “同级”是什么意思?您希望对 Date 列应用什么功能?
  • 嗨@FHTMitchell!如您所见,我有向量:2,3,2,10,11,12,11,3,2,1 我想要向量:2,3,2,2,3,4,3,3,2, 3 覆盖检测到的第一个值中的大值
  • 这仍然不清楚:from the first value detected。请准确解释你的逻辑。

标签: python python-3.x pandas dataframe


【解决方案1】:

我假设您希望权重向量值彼此接近。 你可以试试这个代码

std=np.std(df['weight']) for i,j in df.iterrows(): if (j['weight'] > std): df['weight'][i] = j['weight']/std

输出

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-28
    • 1970-01-01
    • 2014-08-10
    • 2013-02-06
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多