如何通过将另一列中的两个值分组为两个值来添加两列值 [英] How to add two columns of values from grouping two by two values from another column
问题描述
由于要分组文本值(在另一列中具有相同的值),因此我想创建一个新的pandas数据框.例如,我得到了以下数据框:
I would like to create a new pandas data-frame as a result of grouping text values which has the same value in other column. So for instance, I got the following dataframe:
example_dct = {
"text": {
"0": "this is my text 1",
"1": "this is my text 2",
"2": "this is my text 3",
"3": "this is my text 4",
"4": "this is my text 5"
},
"article_id": {
"0": "#0001_01_xml",
"1": "#0001_01_xml",
"2": "#0001_02_xml",
"3": "#0001_03_xml",
"4": "#0001_03_xml"
}
}
df_example = pd.DataFrame.from_dict(example_dct)
print(df_example)
text article_id
0 this is my text 1 #0001_01_xml
1 this is my text 2 #0001_01_xml
2 this is my text 3 #0001_02_xml
3 this is my text 4 #0001_03_xml
4 this is my text 5 #0001_03_xml
我想通过以下方式创建两个新列:
I would like to create two new columns in the following way:
text_1 text_2 article_id
0 'this is my text 1' 'this is my text 2' #0001_01_xml
1 'this is my text 4' 'this is my text 5' #0001_03_xml
如果有> 2个具有相同id值的文本值,例如:
In the case that there is >2 text values with the same id value, example:
example_dct = {
"text": {
"0": "this is my text 1",
"1": "this is my text 2",
"2": "this is my text 3",
"3": "this is my text 4",
"4": "this is my text 5",
"5": "this is my text 6",
},
"article_id": {
"0": "#0001_01_xml",
"1": "#0001_01_xml",
"2": "#0001_02_xml",
"3": "#0001_03_xml",
"4": "#0001_03_xml",
"5": "#0001_03_xml",
}
}
然后输出数据帧应该是将1乘1的文本串联的结果:
Then the output dataframe should be the result of concatenating 1 by 1 texts:
text_1 text_2 article_id
0 'this is my text 1' 'this is my text 2' #0001_01_xml
1 'this is my text 4' 'this is my text 5' #0001_03_xml
2 'this is my text 4' 'this is my text 6' #0001_03_xml
3 'this is my text 5' 'this is my text 6' #0001_03_xml
此外,我想创建另一个与此数据集相似的数据集,但仅使用那些没有通用article_id的列(因此group by的倒数).
示例:
Furthermore, I would like to create another dataset similar to this one, but just with those columns that does not have common article_id (so the inverse of group by).
Example:
text_1 text_2 article_id_1 article_id_2
0 'this is my text 1' 'this is my text 3' #0001_01_xml. "#0001_02_xml"
1 'this is my text 1' 'this is my text 4' #0001_01_xml" #0001_03_xml"
2 'this is my text 1' 'this is my text 5' #0001_01_xml. "#0001_03_xml"
3 'this is my text 1' 'this is my text 6' #0001_01_xml "#0001_03_xml"
4 'this is my text 2' 'this is my text 3' #0001_02_xml "#0001_03_xml"
5 'this is my text 2' 'this is my text 4' #0001_02_xml "#0001_03_xml"
6 'this is my text 2' 'this is my text 5' #0001_02_xml "#0001_03_xml"
7 'this is my text 2' 'this is my text 6' #0001_02_xml "#0001_03_xml"
..
..
..
..
..
有什么主意我该怎么做?
Any ideas how can I make this approach?
推荐答案
在扁平化列表推导中,首次使用每组2个值的组合时,默认情况下会省略1个值的组:
For first use combinations of 2 values per groups in flattened list compreehnsion, there are groups with 1 values omitted by default:
example_dct = {
"text": {
"0": "this is my text 1",
"1": "this is my text 2",
"2": "this is my text 3",
"3": "this is my text 4",
"4": "this is my text 5",
"5": "this is my text 6",
},
"article_id": {
"0": "#0001_01_xml",
"1": "#0001_01_xml",
"2": "#0001_02_xml",
"3": "#0001_03_xml",
"4": "#0001_03_xml",
"5": "#0001_03_xml",
}
}
df = pd.DataFrame.from_dict(example_dct)
from itertools import combinations
L = [y + (name,) for name, x in df.groupby('article_id')['text'] for y in combinations(x, 2)]
df1 = pd.DataFrame(L, columns=['text_1','text_2', 'article_id'])
print(df1)
text_1 text_2 article_id
0 this is my text 1 this is my text 2 #0001_01_xml
1 this is my text 4 this is my text 5 #0001_03_xml
2 this is my text 4 this is my text 6 #0001_03_xml
3 this is my text 5 this is my text 6 #0001_03_xml
因此,如果将值0001_02_xml
更改为0001_03_xml
,则会得到:
So if changed values 0001_02_xml
to 0001_03_xml
get:
example_dct = {
"text": {
"0": "this is my text 1",
"1": "this is my text 2",
"2": "this is my text 3",
"3": "this is my text 4",
"4": "this is my text 5",
"5": "this is my text 6",
},
"article_id": {
"0": "#0001_01_xml",
"1": "#0001_01_xml",
"2": "#0001_03_xml",
"3": "#0001_03_xml",
"4": "#0001_03_xml",
"5": "#0001_03_xml",
}
}
df = pd.DataFrame.from_dict(example_dct)
from itertools import combinations
L = [y + (name,) for name, x in df.groupby('article_id')['text'] for y in combinations(x, 2)]
df1 = pd.DataFrame(L, columns=['text_1','text_2', 'article_id'])
print(df1)
text_1 text_2 article_id
0 this is my text 1 this is my text 2 #0001_01_xml
1 this is my text 3 this is my text 4 #0001_03_xml
2 this is my text 3 this is my text 5 #0001_03_xml
3 this is my text 3 this is my text 6 #0001_03_xml
4 this is my text 4 this is my text 5 #0001_03_xml
5 this is my text 4 this is my text 6 #0001_03_xml
6 this is my text 5 this is my text 6 #0001_03_xml
第二次使用:
df2 = (df.assign(a=1).merge(df.assign(a=1), on='a', suffixes=('_1','_2'))
.merge(df1, indicator=True, how='left')
.query('_merge == "left_only" & article_id_1 != article_id_2')
[['text_1','text_2', 'article_id_1','article_id_2']]
)
print (df2)
text_1 text_2 article_id_1 article_id_2
2 this is my text 1 this is my text 3 #0001_01_xml #0001_02_xml
3 this is my text 1 this is my text 4 #0001_01_xml #0001_03_xml
4 this is my text 1 this is my text 5 #0001_01_xml #0001_03_xml
5 this is my text 1 this is my text 6 #0001_01_xml #0001_03_xml
8 this is my text 2 this is my text 3 #0001_01_xml #0001_02_xml
9 this is my text 2 this is my text 4 #0001_01_xml #0001_03_xml
10 this is my text 2 this is my text 5 #0001_01_xml #0001_03_xml
11 this is my text 2 this is my text 6 #0001_01_xml #0001_03_xml
12 this is my text 3 this is my text 1 #0001_02_xml #0001_01_xml
13 this is my text 3 this is my text 2 #0001_02_xml #0001_01_xml
15 this is my text 3 this is my text 4 #0001_02_xml #0001_03_xml
16 this is my text 3 this is my text 5 #0001_02_xml #0001_03_xml
17 this is my text 3 this is my text 6 #0001_02_xml #0001_03_xml
18 this is my text 4 this is my text 1 #0001_03_xml #0001_01_xml
19 this is my text 4 this is my text 2 #0001_03_xml #0001_01_xml
20 this is my text 4 this is my text 3 #0001_03_xml #0001_02_xml
24 this is my text 5 this is my text 1 #0001_03_xml #0001_01_xml
25 this is my text 5 this is my text 2 #0001_03_xml #0001_01_xml
26 this is my text 5 this is my text 3 #0001_03_xml #0001_02_xml
30 this is my text 6 this is my text 1 #0001_03_xml #0001_01_xml
31 this is my text 6 this is my text 2 #0001_03_xml #0001_01_xml
32 this is my text 6 this is my text 3 #0001_03_xml #0001_02_xml
这篇关于如何通过将另一列中的两个值分组为两个值来添加两列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!