Python 是否可以将所有相同的术语连接到同一数据帧列中?
我有以下大熊猫数据框,它由几个术语组成:Python 是否可以将所有相同的术语连接到同一数据帧列中?,python,python-3.x,pandas,data-structures,Python,Python 3.x,Pandas,Data Structures,我有以下大熊猫数据框,它由几个术语组成: type name exp ------------------- feline tiger True feline cat False rodent rabbit True canine dog False feline puma True feline bobcat False 是否可以将name列中所有在type列中具有相同类型的术语连接到同一单元格中?。例如: type name
type name exp
-------------------
feline tiger True
feline cat False
rodent rabbit True
canine dog False
feline puma True
feline bobcat False
是否可以将name
列中所有在type
列中具有相同类型的术语连接到同一单元格中?。例如:
type name exp
----------------------------------
feline tiger cat puma bobcat True
rodent rabbit True
canine dog False
这里有一条路
In [797]: df.groupby('type', as_index=False).agg({'name': ' '.join, 'exp': 'max'})
Out[797]:
type name exp
0 canine dog False
1 feline tiger cat puma bobcat True
2 rodent rabbit True
这里有一条路
In [797]: df.groupby('type', as_index=False).agg({'name': ' '.join, 'exp': 'max'})
Out[797]:
type name exp
0 canine dog False
1 feline tiger cat puma bobcat True
2 rodent rabbit True
使用
df.groupby
:
In [200]: df_grouped = df.groupby('type', sort=False, as_index=False)
第一个句柄名称
:
In [202]: df_grouped['name'].apply(lambda x: ' '.join(x))
Out[202]:
0 tiger cat puma bobcat
1 rabbit
2 dog
dtype: object
df.groupby('type', sort=False, as_index=False)\
.agg({'name' : lambda x: ' '.join(set(x)), 'exp' : any})
现在,处理exp
:
In [203]: df_grouped['exp'].apply(any)
Out[203]:
0 True
1 True
2 False
dtype: bool
综合起来:
In [219]: df_grouped = df.groupby('type', sort=False, as_index=False).agg({'name' : ' '.join, 'exp' : any}); df_grouped
Out[219]:
type name exp
0 feline tiger cat puma bobcat True
1 rodent rabbit True
2 canine dog False
若要仅保留唯一项,请将lambda传递给
名称
:
In [202]: df_grouped['name'].apply(lambda x: ' '.join(x))
Out[202]:
0 tiger cat puma bobcat
1 rabbit
2 dog
dtype: object
df.groupby('type', sort=False, as_index=False)\
.agg({'name' : lambda x: ' '.join(set(x)), 'exp' : any})
使用
df.groupby
:
In [200]: df_grouped = df.groupby('type', sort=False, as_index=False)
第一个句柄名称
:
In [202]: df_grouped['name'].apply(lambda x: ' '.join(x))
Out[202]:
0 tiger cat puma bobcat
1 rabbit
2 dog
dtype: object
df.groupby('type', sort=False, as_index=False)\
.agg({'name' : lambda x: ' '.join(set(x)), 'exp' : any})
现在,处理exp
:
In [203]: df_grouped['exp'].apply(any)
Out[203]:
0 True
1 True
2 False
dtype: bool
综合起来:
In [219]: df_grouped = df.groupby('type', sort=False, as_index=False).agg({'name' : ' '.join, 'exp' : any}); df_grouped
Out[219]:
type name exp
0 feline tiger cat puma bobcat True
1 rodent rabbit True
2 canine dog False
若要仅保留唯一项,请将lambda传递给
名称
:
In [202]: df_grouped['name'].apply(lambda x: ' '.join(x))
Out[202]:
0 tiger cat puma bobcat
1 rabbit
2 dog
dtype: object
df.groupby('type', sort=False, as_index=False)\
.agg({'name' : lambda x: ' '.join(set(x)), 'exp' : any})
谢谢你的帮助!。。。“max”参数的功能是什么?是否可以添加一个
、
或一个“|”来分隔术语?…我试图”|.连接“
。。。但是它不起作用编辑应该是“|”。加入。谢谢你的帮助!。。。“max”参数的功能是什么?是否可以添加一个、
或一个“|”来分隔术语?…我试图”|.连接“
。。。但是它不起作用编辑应该是“|”。加入。谢谢你的帮助!。。。“max”参数的功能是什么?是否可以添加一个、
或“|”
来分隔术语?…我尝试”|。加入“
。。。然而,事实并非如此worked@J.Do正确的语法是“|”。join
或”,“。join
@J。Do My function使用any
而不是max。max是相同的。基本上,如果组成值中的任何一个为真,它将返回true,否则返回false。@J.Do别忘了,如果有帮助,您可以接受答案:)选择帮助您解决问题的答案。@J.Do Easy:df.groupby('type',sort=false,as_index=false)。agg({name':lambda x:'.join(set(x)),'exp':any})
谢谢您的帮助!。。。“max”参数的功能是什么?是否可以添加一个、
或“|”
来分隔术语?…我尝试”|。加入“
。。。然而,事实并非如此worked@J.Do正确的语法是“|”。join
或”,“。join
@J。Do My function使用any
而不是max。max是相同的。基本上,如果组成值中的任何一个为true,它将返回true,否则返回false。@J.Do别忘了,如果有帮助,您可以接受答案:)选择帮助您解决问题的答案。@J.Do Easy:df.groupby('type',sort=false,as_index=false)。agg({'name':lambda x:'.join(set(x)),'exp':any})