相关疑难解决方法(0)

在保持价值关联的同时对熊猫进行重新取样

从这样的事情开始:

from pandas import DataFrame
time = np.array(('2015-08-01T00:00:00','2015-08-01T12:00:00'),dtype='datetime64[ns]')
heat_index = np.array([101,103])
air_temperature = np.array([96,95])

df = DataFrame({'heat_index':heat_index,'air_temperature':air_temperature},index=time)
Run Code Online (Sandbox Code Playgroud)

产生这个df:

                     air_temperature    heat_index
2015-08-01 07:00:00  96                 101
2015-08-01 19:00:00  95                 103
Run Code Online (Sandbox Code Playgroud)

然后每天重新采样:

df_daily = df.resample('24H',how='max')
Run Code Online (Sandbox Code Playgroud)

为此得到df_daily:

            air_temperature     heat_index
2015-08-01  96                  103
Run Code Online (Sandbox Code Playgroud)

因此,通过how='max'每24小时使用pandas重新采样重新采样,从每列中获取该时间段内的最大值.

但正如你所看到的看着df输出2015-08-01,当天的最大热指数(发生在19:00:00)不相关的空气温度发生在同一时间.也就是说,在空气温度为95°F时引起103°的热指数.这种关联通过重新取样而丢失,我们最终会从一天中的不同时间看到空气温度.

有没有办法只重新采样一列,并将值保留在同一索引的另一列中?所以最终结果如下:

            air_temperature     heat_index
2015-08-01  95                  103
Run Code Online (Sandbox Code Playgroud)

我的第一个猜测就是重新对该heat_index列进行重新采样......

df_daily = df.resample('24H',how={'heat_index':'max'})
Run Code Online (Sandbox Code Playgroud)

要得到...

            air_temperature
2015-08-01  103
Run Code Online (Sandbox Code Playgroud)

...然后尝试从那里做某种DataFrame.loc或DataFrame.ix,但一直没有成功.关于如何在重新采样后找到相关值的任何想法(例如,找到与air_temperature后来发现的最大值同时发生的值heat_index)?

python datetime pandas

5
推荐指数
1
解决办法
1944
查看次数

在 groupby 之后获得最低值 - Pandas

我有一个具有以下格式的表格:

data = {'City' : ['London', 'Paris', 'Paris','NY' 'London'], 'Distance' : [5, 1, 7, 2, 6]}
df = pd.DataFrame(data)
df

    City    Distance
0   London  5
1   Paris   1
2   Paris   7
3   NY      2
4   London  6
Run Code Online (Sandbox Code Playgroud)

我想创建一个所有行都具有唯一“城市”的表,并且每当有 2 行或更多行具有相同的“城市”值时,我希望它返回具有最低“距离”的行。所以在这种情况下,我想要一个这样的表:

City    Distance
London  5
Paris   1
NY      2
Run Code Online (Sandbox Code Playgroud)

我知道我可以使用:

df.groupby('City')
Run Code Online (Sandbox Code Playgroud)

但我不知道要添加什么才能返回最小的“距离”。

最好的,罗莎

python pandas

3
推荐指数
2
解决办法
3533
查看次数

获取分组中具有最大值的行

我有一个根据id-column分组的数据框。对于每个组,我想获取包含最大值的行(整行,而不仅仅是值)。我可以通过首先获取每个组的最大值,然后创建一个过滤器数组,然后在原始数据帧上应用过滤器来做到这一点。像这样,

import pandas as pd

# Dummy data
df = pd.DataFrame({'id' : [1, 1, 1, 2, 2, 2, 3, 3, 4, 4, 4, 4],
                   'other_value' : ['a', 'e', 'b', 'b', 'a', 'd', 'b', 'f' ,'a' ,'c', 'e', 'f'],
                   'value' : [1, 3, 5, 2, 5, 6, 2, 4, 6, 1, 7, 3]
                   })

# Get the max value in each group
df_max = df.groupby('id')['value'].max()

# Create row filter
row_filter = [df_max[i]==v for i, v in zip(df['id'], df['value'])]

# …
Run Code Online (Sandbox Code Playgroud)

python pandas pandas-groupby

3
推荐指数
1
解决办法
1万
查看次数

在一列上对数据框进行分组,并从一列中获取最大值,并从另一列中获取其对应的值

我有一个大数据框,它具有类似的模式,如下所示:

    X   Y   Z
0   a   p   2
1   a   q   5
2   a   r   6
3   a   s   3
4   b   w   10
5   b   z   20
6   b   y   9
7   b   x   20
Run Code Online (Sandbox Code Playgroud)

并且可以构造为:

df = {
    'X': ['a', 'a', 'a', 'a', 'b', 'b', 'b', 'b'],
    'Y': ['p', 'q', 'r', 's', 'w', 'x', 'y', 'z'],
    'Z': [2, 5, 6, 3, 10, 20, 9, 5]
}
Run Code Online (Sandbox Code Playgroud)

现在,我想这组数据帧由第一列即X采取maxZ柱和其对应的价值Y。如果 中有两个最大值Z,那么我想从 …

python dataframe pandas pandas-groupby

3
推荐指数
1
解决办法
65
查看次数

如何通过pandas获取多列组的最大值?

我试图根据groupby的另一列获取最大值的行,我试图遵循这里给出的解决方案Python:使用groupby获取具有最大值的行,但是当你申请时它不起作用

annotations.groupby(['bookid','conceptid'], sort=False)['weight'].max()
Run Code Online (Sandbox Code Playgroud)

我明白了

bookid    conceptid
12345678  3942     0.137271
          10673    0.172345
          1002     0.125136
34567819  44407    1.370921
          5111     0.104729
          6160     0.114766
          200      0.151629
          3504     0.152793
Run Code Online (Sandbox Code Playgroud)

但是我想只获得权重最高的行,例如:

bookid    conceptid
12345678  10673    0.172345
34567819  44407    1.370921
Run Code Online (Sandbox Code Playgroud)

我很感激任何帮助

python group-by pandas

2
推荐指数
1
解决办法
3750
查看次数

在 Pandas 数据框中按组过滤具有最小值的行

我刚刚过滤了一些数据,现在我有一个 .csv 文件,但我注意到我只需要选择具有最低价格的行:

例子:

ORIGIN   | DESTINA. | PRICE
____________________________
BOG      | MAD      |  1500
BOG      | MAD      |  750
BOG      | MAD      |  1250
BOG      | MAD      |  1350
BOG      | MIA      |   450
Run Code Online (Sandbox Code Playgroud)

所以在这个例子中,我想得到的只是第三行和第六行:

ORIGIN   | DESTINA. | PRICE
____________________________
BOG      | MAD      | 750
BOG      | MIA      | 450
Run Code Online (Sandbox Code Playgroud)

使用 python,我怎样才能得到这个决赛桌?

python group-by aggregate pandas pandas-groupby

2
推荐指数
1
解决办法
3108
查看次数

Python Pandas groupby forloop和Idxmax

我有一个DataFrame,必须在三个级别上分组,然后返回最高值.每天都有每个独特价值的回报,我想找到最高的回报和细节.

data.groupby(['Company','Product','Industry'])['ROI'].idxmax()
Run Code Online (Sandbox Code Playgroud)

回报表明:

Target   - Dish Soap - House       had a 5% ROI on 9/17
Best Buy - CDs       - Electronics had a 3% ROI on 9/3
Run Code Online (Sandbox Code Playgroud)

是最高的.

这是一些示例数据:

+----------+-----------+-------------+---------+-----+
| Industry | Product   | Industry    | Date    | ROI |
+----------+-----------+-------------+---------+-----+
| Target   | Dish Soap | House       | 9/17/13 | 5%  |
| Target   | Dish Soap | House       | 9/16/13 | 2%  |
| BestBuy  | CDs       | Electronics | 9/1/13  | 1%  |
| BestBuy  | …
Run Code Online (Sandbox Code Playgroud)

python for-loop pandas

1
推荐指数
1
解决办法
2233
查看次数

从python中的groupby对象中选择一个特定的行

id    marks  year 
1     18      2013
1     25      2012
3     16      2014
2     16      2013
1     19      2013
3     25      2013
2     18      2014
Run Code Online (Sandbox Code Playgroud)

假设现在我通过python命令将上面的id分组.
grouped = file.groupby(file.id)

我想获得一个新文件,每个组中只有一行,最近一年是该组中一年中最高的一年.

请告诉我这个命令,我正在尝试使用apply但它只给出了布尔表达式.我想要最新一年的整行.

python group-by pandas

1
推荐指数
1
解决办法
7702
查看次数