Column to row in pandas dataframe

Anonymous

I would like to use a couple of columns as row ID while taking count of grouping based on Time. Look at below illustration:

X Y Z Time
0 1 2  10
0 2 3  10
1 0 2  15
1 0 0  23

Transforms into this:

Category Count Time
   X       0    10
   X       1    15
   X       1    23
   Y       3    10
   Y       0    15
   Y       0    23
   Z       5    10
   Z       2    15
   Z       0    23

What is happening is that X occur 0 times for the time 10 but 1 time for 15 and 23.
Y occur 3 times at 10'clock but none at 15 and 23. etc.

jezrael

I think you need melt with groupby aggregating sum and last sort_values by column Category:

print pd.melt(df, id_vars='Time', var_name='Category', value_name='Count')
        .groupby(['Time','Category']).sum().reset_index().sort_values('Category')
   Time Category  Count
0    10        X      0
3    15        X      1
6    23        X      1
1    10        Y      3
4    15        Y      0
7    23        Y      0
2    10        Z      5
5    15        Z      2
8    23        Z      0

Another solution with stack:

df1 = df.set_index('Time')
        .stack()
        .groupby(level=[0,1])
        .sum()
        .reset_index()
        .sort_values('level_1')

df1.columns = ['Time','Category','Count']
df1 = df1[['Category','Count','Time']]
print df1
  Category  Count  Time
0        X      0    10
3        X      1    15
6        X      1    23
1        Y      3    10
4        Y      0    15
7        Y      0    23
2        Z      5    10
5        Z      2    15
8        Z      0    23

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Normalizing a pandas DataFrame by row

来自分类Dev

Pandas DataFrame column concatenation

来自分类Dev

Splitting a pandas dataframe column by delimiter

来自分类Dev

Combine columns in a Pandas DataFrame to a column of lists in a DataFrame

来自分类Dev

Python summarize row into column (Pandas pivot table)

来自分类Dev

基于Column值的Pandas DataFrame操作

来自分类Dev

Pandas DataFrame column to cell in pivot table

来自分类Dev

Construct Pandas DataFrame from dictionary in form {index: list of row values}

来自分类Dev

将变量放入特定位置 [row,column] Pandas Python

来自分类Dev

如果column是列表,是否可以过滤Pandas DataFrame列?

来自分类Dev

How do I turn a Pandas DataFrame object with 1 main column into a Pandas Series with the index column from the original DataFrame

来自分类Dev

仅从Pandas DataFrame中过滤出真值,返回(Row,Col)的元组

来自分类Dev

Column/Row index in a DataGrid column

来自分类Dev

$ row ['column']在PHP中

来自分类Dev

如何在Pandas Dataframe中获取行并转换为Column的值?

来自分类Dev

Convert wide-form pandas DataFrame with hierarchical column index to tidy format

来自分类Dev

在dataframe Column上应用函数以获取其他几个列Pandas Python

来自分类Dev

Pandas: dataframe to long format

来自分类Dev

自然排序Pandas DataFrame

来自分类Dev

Pandas multiIndex DataFrame sort

来自分类Dev

Pandas DataFrame列串联

来自分类Dev

使用Pandas扩展DataFrame

来自分类Dev

plot dataframe pandas not working

来自分类Dev

迭代创建pandas DataFrame

来自分类Dev

循环添加Pandas DataFrame

来自分类Dev

Pandas Dataframe的Web抓取

来自分类Dev

计算Pandas Dataframe的整列

来自分类Dev

熊猫系列的Pandas DataFrame

来自分类Dev

Pandas DataFrame的空副本

Related 相关文章

热门标签

归档