小编Con*_*111的帖子

熊猫按结果分组到列

我有这样的数据框:

x = pd.DataFrame({
    'audio': ['audio1', 'audio1', 'audio2', 'audio2', 'audio3', 'audio3'],
    'text': ['text1', 'text2', 'text3', 'text4', 'text5', 'text6'],
    'login': ['operator1', 'operator2', 'operator3', 'operator4', 'operator5', 'operator6'] 
})
Run Code Online (Sandbox Code Playgroud)

我正在尝试像这样聚合它:

x1 = x.groupby('audio')['text'].agg(
    [
    ('text1', lambda x : x.iat[0]),
    ('text2', lambda x : x.iat[1]),
    ('leven', lambda x: Levenshtein.distance(x.iat[0], x.iat[1])) #some function works with grouped text
    ]
).reset_index()
Run Code Online (Sandbox Code Playgroud)

它可以工作,但我还需要将分组登录添加到行,以使行像这样:

audio, text1, text2, leven, login1, login2
Run Code Online (Sandbox Code Playgroud)

我试过类似的东西,lambda x : x.ait[0, 1]但它不起作用

python dataframe pandas

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

熊猫,复制名称重复N次的行

示例数据:

df1 = pd.DataFrame({
    'file': ['file1','file1','file1','file2','file2','file2','file3','file3','file4'],
    'prop1': [True,False,True,False,False,False,True,False,False],
    'prop2': [False,False,False,False,True,False,False,True,False],
    'prop3': [False,True,False,True,False,True,False,False,True]
})
Run Code Online (Sandbox Code Playgroud)

我需要复制其“文件”重复3次的行,以获得以下内容:

file    prop1   prop2   prop3
0   file1   True    False   False
1   file1   False   False   True
2   file1   True    False   False
3   file2   False   False   True
4   file2   False   True    False
5   file2   False   False   True
Run Code Online (Sandbox Code Playgroud)

python dataframe pandas

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

标签 统计

dataframe ×2

pandas ×2

python ×2