MATLAB Answers

How to create a pivot table

5 ビュー (過去 30 日間)
alpedhuez
alpedhuez 2020 年 6 月 29 日
編集済み: alpedhuez 2020 年 7 月 3 日
I have a table
date person spendings
----------------------------------------
1/1/2020 John $100
1/1/2020 Mike $50
1/2/2020 John $75
1/3/2020 Mike $40
I want to create a pivot table
John Spendings Mike Spendings
----------------------------------------------------------------------------------
January 2020
February 2020
March 2020
Please advise.

  1 件のコメント

alpedhuez
alpedhuez 2020 年 6 月 29 日
Is it that this example is too trivial or is it that Matlab is not good at it?

サインインしてコメントする。

採用された回答

Maadhav Akula
Maadhav Akula 2020 年 7 月 2 日
I think the unstack function might help you out. I have written a small example using your data:
month = categorical({'Jan';'Jan';'Feb';'March'});
person = categorical({'John';'Mike';'John';'Mike'});
spendings = [100;50;75;40];
T = table(month,person,spendings)
u = unstack(T,'spendings','person')
I think you can customise it according to your needs.
Hope this helps!

  3 件のコメント

alpedhuez
alpedhuez 2020 年 7 月 2 日
The orginal table has dd/mm/yyyy not month. This is the point.
Maadhav Akula
Maadhav Akula 2020 年 7 月 2 日
You can utilize the month and datetime functions to tweak them to your required format.
a = month(datetime('1/1/2020','InputFormat','dd/MM/yyyy'),'shortname')
The above line returns the Month in 'Mmm' format, for instance January as 'Jan'. You can tweak some of these properties to obtain the required values which best suits your requirements.
alpedhuez
alpedhuez 2020 年 7 月 3 日
date=datetime(T2.yyyy_mm,'InputFormat','yyyy-MM');

サインインしてコメントする。

その他の回答 (0 件)

タグ

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by