如何将可变数量的“夫妇”列表转换为两列数据框?

时间:2019-02-26 18:07:06

标签: python pandas dataframe

如何从具有两列(auth1和auth2)的列表的共同作者列表中生成数据框 每对夫妇一行?

coauthors = []
coauthors.append((("f","g"),("f","h"),("g","h"))) # combinations of f,g,h
coauthors.append((("i","j"),("i","k"),("i","l"),("j","k"),("j","l"),("l","k"))) # combinations of i,j,k,l
coauthors.append((("a","b"))) # combinations of a,b
for s in coauthors:
   print(*s)

侧面问题:为什么上面打印的最后一行不是('a','b'),而是(b)?

2 个答案:

答案 0 :(得分:0)

您应该使用 <input (click)="focusOut()" type="text" matInput [formControl]="inputControl" [matAutocomplete]="auto"> <mat-autocomplete #auto="matAutocomplete" [displayWith]="displayFn" > <mat-option (onSelectionChange)="submitValue($event)" *ngFor="let option of options | async" [value]="option"> {{option.name | translate}} </mat-option> </mat-autocomplete> TS focusOut() { this.inputControl.disable(); this.inputControl.enable(); } 而不是[26-Feb-2019 18:10:48 UTC] PHP Fatal error: Uncaught Symfony\Component\Debug\Exception\FatalThrowableError: syntax error, unexpected 'endif' (T_ENDIF), expecting end of file in /home2/website/website.com/storage/framework/views/6a75bc263740fca85e4609e4affe4f57142c2c3a.php:181 Stack trace: #0 /home2/website/website.com/vendor/laravel/framework/src/Illuminate/View/Engines/CompilerEngine.php(59): Illuminate\View\Engines\PhpEngine->evaluatePath('/home2/website...', Array) #1 /home2/website/website.com/vendor/laravel/framework/src/Illuminate/View/View.php(142): Illuminate\View\Engines\CompilerEngine->get('/home2/website...', Array) #2 /home2/website/website.com/vendor/laravel/framework/src/Illuminate/View/View.php(125): Illuminate\View\View->getContents() #3 /home2/website/website.com/vendor/laravel/framework/src/Illuminate/View/View.php(90): Illuminate\View\View->renderContents() #4 /home2/website/website.com/storage/framework/views/e5516c2faf25e2e2a432f5486f5485d6792013e9.php(53): Illuminate\View\View->render() #5 /home2/website/website.com/vendo in /home2/website/website.com/storage/framework/views/6a75bc263740fca85e4609e4affe4f57142c2c3a.php on line 181 [26-Feb-2019 18:10:48 UTC] PHP Fatal error: Uncaught Symfony\Component\Debug\Exception\FatalThrowableError: syntax error, unexpected 'endif' (T_ENDIF), expecting end of file in /home2/website/website.com/storage/framework/views/6a75bc263740fca85e4609e4affe4f57142c2c3a.php:181 Stack trace: #0 /home2/website/website.com/vendor/laravel/framework/src/Illuminate/View/Engines/CompilerEngine.php(59): Illuminate\View\Engines\PhpEngine->evaluatePath('/home2/website...', Array) #1 /home2/website/website.com/vendor/laravel/framework/src/Illuminate/View/View.php(142): Illuminate\View\Engines\CompilerEngine->get('/home2/website...', Array) #2 /home2/website/website.com/vendor/laravel/framework/src/Illuminate/View/View.php(125): Illuminate\View\View->getContents() #3 /home2/website/website.com/vendor/laravel/framework/src/Illuminate/View/View.php(90): Illuminate\View\View->renderContents() #4 /home2/website/website.com/storage/framework/views/e5516c2faf25e2e2a432f5486f5485d6792013e9.php(53): Illuminate\View\View->render() #5 /home2/website/website.com/vendo in /home2/website/website.com/storage/framework/views/6a75bc263740fca85e4609e4affe4f57142c2c3a.php on line 181 来构建原始列表:

extend

这具有通过传递的iterable元素扩展原始列表的作用,而不是将传递的iterable作为原始列表的单个新元素附加。

还要注意,我在上面的最后一行中添加了一个逗号(以解决您的副问题中的问题)。这是为了告诉Python,您希望将一个元组的元组传递给append(外部元组仅包含一个元组)。如果没有这个逗号,则忽略外括号,Python认为您仅通过元组coauthors = [] coauthors.extend((("f","g"),("f","h"),("g","h"))) # combinations of f,g,h coauthors.extend((("i","j"),("i","k"),("i","l"),("j","k"),("j","l"),("l","k"))) # combinations of i,j,k,l coauthors.extend((("a","b"),)) # combinations of a,b

这给出了长度为2的10个元组的列表,而不是使用extend生成的嵌套结构。从这里开始,创建DataFrame很容易:

('a', 'b')

给予:

append

答案 1 :(得分:0)

很难告诉您是否具有这些值,或对组合进行硬编码。但是,如果使用itertools,则可以轻松得多。创建一个列表列表,每个子列表都是您要从中创建配对的作者的分组,然后使用链和组合将所有配对放入DataFrame

import pandas as pd
from itertools import combinations, chain
groups = [['f', 'g', 'h'], ['i', 'j', 'k', 'l'], ['a', 'b']]

pd.DataFrame(chain.from_iterable([combinations(x, 2) for x in groups]),
             columns=['auth1', 'auth2'])

输出:

  auth1 auth2
0     f     g
1     f     h
2     g     h
3     i     j
4     i     k
5     i     l
6     j     k
7     j     l
8     k     l
9     a     b

如果还需要为每个组添加唯一的ID,则可以合并一堆较小的DataFrames

pd.concat([
    pd.DataFrame(data, columns=['auth1', 'auth2']).assign(id=gid) 
        for data,gid in zip([combinations(x, 2) for x in groups], range(len(groups)))
], ignore_index=True)

  auth1 auth2  id
0     f     g   0
1     f     h   0
2     g     h   0
3     i     j   1
4     i     k   1
5     i     l   1
6     j     k   1
7     j     l   1
8     k     l   1
9     a     b   2

或者,您可以将内容解包到3个元素列表的列表中,并调用pd.DataFrame一次:

pd.DataFrame([[*z, gid] for data,gid in zip([combinations(x, 2) for x in groups], range(len(groups))) for z in data],
              columns=['auth1', 'auth2', 'id'])