熊猫:从多行到单行的观察

时间:2018-10-19 17:55:07

标签: python pandas

假设我有这个数据框:

df = pd.DataFrame({'index':['10a','10a','10a','20b','20b','20b','30c','30c','30c']
                   ,'var_vals': ['aaa','aaa','abb','bbb','bba','bbb','ccc','ccc','cab']
                   ,'var2_vals':['aga','aga','add','bgb','bbd','bgb','cdd','cdd','cda']})
display(df)

看起来像这样:

    index   var_vals    var2_vals
0   10a     aaa         aga
1   10a     aaa         aga
2   10a     abb         add
3   20b     bbb         bgb
4   20b     bba         bbd
5   20b     bbb         bgb
6   30c     ccc         cdd
7   30c     ccc         cdd
8   30c     cab         cda

如何将输出变成一行,而只有新列中的不同之处?

    index   var_vals     var_vals_0     var2_vals    var2_vals_0
0   10a     aaa             abb          aga            add
1   20b     bbb             bba          bgb            bbd
2   30c     ccc             cab          cdd            cda

我尝试了groupby,pivot / pivot_table,堆栈/ unstack和融合,但是我要么以巨大的维度结束,要么丢失了数据。

4 个答案:

答案 0 :(得分:3)

通过groupby.apply的一种方法:

df.groupby('index')['var_vals'].apply(lambda x: pd.Series(x.unique())).unstack()

         0    1
index          
10a    aaa  abb
20b    bbb  bba
30c    ccc  cab

答案 1 :(得分:3)

pivotdf.drop_duplicates().assign(key=lambda x : x.groupby('index').cumcount()).pivot('index','key','var_vals') Out[910]: key 0 1 index 10a aaa abb 20b bbb bba 30c ccc cab 一起使用

require_once ('Libraries/jpgraph/jpgraph.php');
require_once ('Libraries/jpgraph/jpgraph_line.php');
require_once ('Libraries/jpgraph/jpgraph_bar.php');

function readsunspotdata($aFile, &$aYears, &$aSunspots) {
    $lines = @file($aFile,FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);
    if( $lines === false ) {
        throw new JpGraphException('Can not read sunspot data file.');
    }
    foreach( $lines as $line => $datarow ) {
        $split = preg_split('/[\s]+/',$datarow);
        $aYears[] = substr(trim($split[0]),0,4);
        $aSunspots[] = trim($split[1]);
    }
}

$year = array();
$ydata = array();
readsunspotdata('yearssn.txt',$year,$ydata);

 // Width and height of the graph
$width = 1000; $height = 500;

// Create a graph instance
$graph = new Graph($width,$height);

// Specify what scale we want to use,
// int = integer scale for the X-axis
// int = integer scale for the Y-axis
$graph->SetScale('intint');

// Setup a title for the graph
$graph->title->Set('Sunspot example');

// Setup titles and X-axis labels
$graph->xaxis->title->Set('(year from 1701)');

// Setup Y-axis title
$graph->yaxis->title->Set('(# sunspots)');

// Create the linear plot
$lineplot=new LinePlot($ydata);

// Add the plot to the graph
$graph->Add($lineplot);

// Display the graph
$graph->Stroke();

答案 2 :(得分:3)

这里是另一个:

newdf = pd.DataFrame(df.groupby('index')['var_vals'].unique().tolist()).fillna('')
  1. tolist()将数据传递回列表格式,这使我们能够重新创建数据帧,并将其再次传递给pd.DataFrame()
  2. fillna处理您可以拥有不同数量唯一性的事实。

更新的代码:

dfs = (pd.DataFrame(df.groupby('index')[i].unique().tolist()).fillna('').add_prefix(i+'_')
        for i in df.drop('index', 1))
df = pd.concat(dfs, axis=1)

完整示例

将熊猫作为pd导入

df = pd.DataFrame({'index':['10a','10a','10a','20b','20b','20b','30c','30c','30c']
                   ,'var_vals': ['aaa','aaa','abb','bbb','bba','bbb','ccc','ccc','cab']
                   ,'var2_vals':['aga','aga','add','bgb','bbd','bgb','cdd','cdd','cda']})

df = pd.concat(
    (pd.DataFrame(df.groupby('index')[i].unique().tolist()).fillna('').add_prefix(i+'_')
    for i in df.drop('index', 1)), axis=1)

print(df)

返回:

  var2_vals_0 var2_vals_1 var_vals_0 var_vals_1
0         aga         add        aaa        abb
1         bgb         bbd        bbb        bba
2         cdd         cda        ccc        cab

答案 3 :(得分:3)

使用默认构造函数的另一种方法

x = df.drop_duplicates().groupby('index').var_vals.agg(list).to_dict()
pd.DataFrame(x).T

    0   1
10a aaa abb
20b bbb bba
30c ccc cab

时间(我猜有点相似):

df = pd.concat([df]*10000).reset_index(drop=True)

%%timeit
x = df.drop_duplicates().groupby('index').var_vals.agg(list).to_dict()
pd.DataFrame(x).T
7.92 ms ± 224 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

%%timeit 
df.drop_duplicates().assign(key=lambda x : x.groupby('index').cumcount()).pivot('index','key','var_vals')
8.81 ms ± 74.9 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

%%timeit
df.groupby('index')['var_vals'].apply(lambda x: pd.Series(x.unique())).unstack()
8.83 ms ± 187 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

%%timeit
pd.DataFrame(df.groupby('index')['var_vals'].unique().tolist())
13.3 ms ± 705 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)