gpt4 book ai didi

python - 数据框中行和列之间的交互

转载 作者:太空狗 更新时间:2023-10-30 01:50:18 24 4
gpt4 key购买 nike

我有一个数据框:

df = pd.DataFrame({
'exam': [
'French', 'English', 'German', 'Russian', 'Russian',
'German', 'German', 'French', 'English', 'French'
],

'student' : ['john', 'ted', 'jason', 'marc', 'peter', 'bob',
'robert', 'david', 'nik', 'kevin'
]
})

print (df)

exam student
0 French john
1 English ted
2 German jason
3 Russian marc
4 Russian peter
5 German bob
6 German robert
7 French david
8 English nik
9 French kevin

有谁知道如何创建一个包含两列“student”和“student shared exam with”的新数据框。

我应该得到类似的东西:

                student   shared_exam_with      
0 john david
1 john kevin
2 ted nik
3 jason bob
4 jason robert
5 marc peter
6 peter marc
7 bob jason
8 bob robert
9 robert jason
10 robert bob
11 david john
12 david kevin
13 nik ted
14 kevin john
15 kevin david

例如:John 学了法语......还有 David 和 Kevin 也学了!

最佳答案

合并

df.merge(
df, on='exam',
suffixes=['', '_shared_with']
).query('student != student_shared_with')

exam student student_shared_with
1 French john david
2 French john kevin
3 French david john
5 French david kevin
6 French kevin john
7 French kevin david
10 English ted nik
11 English nik ted
14 German jason bob
15 German jason robert
16 German bob jason
18 German bob robert
19 German robert jason
20 German robert bob
23 Russian marc peter
24 Russian peter marc

加入

d1 = df.set_index('exam')
d1.join(
d1, rsuffix='_shared_with'
).query('student != student_shared_with')

student student_shared_with
exam
English ted nik
English nik ted
French john david
French john kevin
French david john
French david kevin
French kevin john
French kevin david
German jason bob
German jason robert
German bob jason
German bob robert
German robert jason
German robert bob
Russian marc peter
Russian peter marc

itertools.permutations + groupby

from itertools import permutations as perm

cols = ['student', 'student_shared_with']
df.groupby('exam').student.apply(
lambda x: pd.DataFrame(list(perm(x, 2)), columns=cols)
).reset_index(drop=True)

student student_shared_with
0 ted nik
1 nik ted
2 john david
3 john kevin
4 david john
5 david kevin
6 kevin john
7 kevin david
8 jason bob
9 jason robert
10 bob jason
11 bob robert
12 robert jason
13 robert bob
14 marc peter
15 peter marc

关于python - 数据框中行和列之间的交互,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43378580/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com