相关疑难解决方法(0)

Pandas`DataFrameGroupBy`和`SeriesGroupBy`

我承认我不是一个Python大师,但我仍然觉得处理Pandas DataFrameGroupBySeriesGroupBy对象异常违反直觉.(我有一个R背景.)

我有以下数据框:

import pandas as pd
import numpy as np
df = pd.DataFrame({'id' : range(1,9),
                   'code' : ['one', 'one', 'two', 'three',
                             'two', 'three', 'one', 'two'],
                   'colour': ['black', 'white','white','white',
                           'black', 'black', 'white', 'white'],
                   'irrelevant1': ['foo', 'foo', 'foo','bar','bar',
                                     'foo','bar','bar'],
                   'irrelevant2': ['foo', 'foo', 'foo','bar','bar',
                                     'foo','bar','bar'],
                   'irrelevant3': ['foo', 'foo', 'foo','bar','bar',
                                     'foo','bar','bar'],
                   'amount' : np.random.randn(8)},  columns= ['id','code','colour', 'irrelevant1', 'irrelevant2', 'irrelevant3', 'amount'])
Run Code Online (Sandbox Code Playgroud)

我希望能够按照和id分组.下面的代码进行分组,但保留所有列.codecolour

gb = df.groupby(['code','colour'])
gb.head(5)
                id   code colour irrelevant1 irrelevant2 irrelevant3    amount
code  colour                                                                  
one   black …
Run Code Online (Sandbox Code Playgroud)

python group-by pandas

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

Python:尝试交叉应用两个数据帧

我试图获得一个数据帧,其中包含两个不同数据帧中两个独立列的所有组合.我的数据框看起来像这样:

>>>first_df                          >>>second_df
    id test                                id text   
 0   1  abc                              0 11  uvw
 1   2  def                              1 22  xyz 
 2   3  ghi
Run Code Online (Sandbox Code Playgroud)

由此,我能够使用这种方法获得组合:

df = pd.DataFrame(list(itertools.product(list(a['test']),list(b['text']))),columns=['test','text'])
>>>df
    test text
 0  abc  uvw
 1  abc  xyz
 2  def  uvw
 3  def  xyz
 4  ghi  uvw
 5  ghi  xyz
Run Code Online (Sandbox Code Playgroud)

我无法理解的是,如何将相关的id列也添加到我的数据框中,如下所示:

>>>df
    id test text kid
 0   1 abc  uvw   11
 1   1 abc  xyz   22
 2   2 def  uvw   11
 3   2 def  xyz   22
 4   3 ghi  uvw   11
 5   3 …
Run Code Online (Sandbox Code Playgroud)

python apply dataframe pandas

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

从数据框中查找列的唯一组合

在下面的数据集中,我需要找到唯一的序列并为其分配一个序列号..

数据集:

user    age maritalstatus   product
A   Young   married 111
B   young   married 222
C   young   Single  111
D   old single  222
E   old married 111
F   teen    married 222
G   teen    married 555
H   adult   single  444
I   adult   single  333
Run Code Online (Sandbox Code Playgroud)

预期产量:

young   married     0
young   single      1
old     single      2
old     married     3
teen    married     4
adult   single      5
Run Code Online (Sandbox Code Playgroud)

找到上面显示的唯一值后,如果我通过下面的新用户,

user age maritalstatus  
X     young  married 
Run Code Online (Sandbox Code Playgroud)

它应该把产品退给我。

X : [111, 222]
Run Code Online (Sandbox Code Playgroud)

如果没有顺序,如下所示

user     age     maritalstatus  
    Y     adult  married …
Run Code Online (Sandbox Code Playgroud)

python pandas

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

pandas中有多少列,python?

有人知道pandas,python中的总列数吗?我刚刚为包含超过20,000列的pandas创建了一个数据帧,但是我遇到了内存错误.

非常感谢

python pandas

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

标签 统计

pandas ×4

python ×4

apply ×1

dataframe ×1

group-by ×1