这是熊猫df:
safeHead
我想用Measure中的值“传播” Group变量。
init'
final String[] columns = {MediaStore.Images.Media.DATA,
MediaStore.Images.Media._ID,
MediaStore.Images.ImageColumns.DATE_TAKEN,
MediaStore.Images.ImageColumns.DISPLAY_NAME,
MediaStore.Images.Thumbnails.DATA};
final String orderBy = MediaStore.Images.ImageColumns.DATE_TAKEN;
//Stores all the images from the gallery in Cursor
Cursor cursor = context.getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null,
null, orderBy + " DESC ");
变量中的每个级别变为其自己的列,该列填充了列df = pd.DataFrame({'First' : ['John', 'Jane', 'Mary'],
'Last' : ['Smith', 'Doe', 'Johnson'],
'Group' : ['A', 'B', 'A'],
'Measure' : [2, 11, 1]})
df
Out[38]:
First Last Group Measure
0 John Smith A 2
1 Jane Doe B 11
2 Mary Johnson A 1
中包含的值。我该如何实现?
答案 0 :(得分:5)
使用pivot_table
df.pivot_table(index=['First','Last'],columns='Group',values='Measure',fill_value=0)
Out[247]:
Group A B
First Last
Jane Doe 0 11
John Smith 2 0
Mary Johnson 1 0
答案 1 :(得分:2)
如果您的订单无关紧要,则可以按照以下步骤进行操作:
df.set_index(['First','Last', 'Group']).unstack('Group').fillna(0).reset_index()
First Last Measure
Group A B
0 Jane Doe 0.0 11.0
1 John Smith 2.0 0.0
2 Mary Johnson 1.0 0.0