Python 重新索引存储箱索引数据帧

Python 重新索引存储箱索引数据帧,python,pandas,pandas-groupby,Python,Pandas,Pandas Groupby,我希望在应用groupby后,为索引/列中未显示的容器添加带零的行和列: import numpy as np import pandas as pd bins = np.arange(-0.1, 2, 0.1) names = np.random.random_integers(0, 100, 1000) a = np.random.random(1000) b = np.random.random(1000) matrix = pd.DataFrame([names, pd.cut(a

我希望在应用groupby后,为索引/列中未显示的容器添加带零的行和列:

import numpy as np
import pandas as pd

bins = np.arange(-0.1, 2, 0.1)

names = np.random.random_integers(0, 100, 1000)
a = np.random.random(1000)
b = np.random.random(1000)

matrix = pd.DataFrame([names, pd.cut(a, bins), pd.cut(b, bins)]).T
matrix.columns = ['names', 'a', 'b']

matrix = matrix.groupby(['a', 'b']).count()
matrix.reset_index(inplace=True)

matrix = matrix.pivot(index='a', columns='b', values='names').fillna(0)

pandas.cut方法的输出分配给变量以访问属性:

bins = np.arange(-0.1, 2, 0.1)

names = np.random.random_integers(0, 100, 1000)
a = np.random.random(1000)
b = np.random.random(1000)


##############################
# Use pd.cut like this
a_bins = pd.cut(a, bins)
b_bins = pd.cut(b, bins)
##############################


matrix = pd.DataFrame([names, a_bins, b_bins]).T


matrix.columns = ['names', 'a', 'b']

matrix = matrix.groupby(['a', 'b']).count()
matrix.reset_index(inplace=True)

matrix = matrix.pivot(index='a', columns='b', values='names').fillna(0)

##################################################
# Reindex with this
matrix = matrix.reindex(index=a_bins.categories,
                        columns=b_bins.categories,
                        fill_value=0)
##################################################

pandas.cut方法的输出分配给变量以访问属性:

bins = np.arange(-0.1, 2, 0.1)

names = np.random.random_integers(0, 100, 1000)
a = np.random.random(1000)
b = np.random.random(1000)


##############################
# Use pd.cut like this
a_bins = pd.cut(a, bins)
b_bins = pd.cut(b, bins)
##############################


matrix = pd.DataFrame([names, a_bins, b_bins]).T


matrix.columns = ['names', 'a', 'b']

matrix = matrix.groupby(['a', 'b']).count()
matrix.reset_index(inplace=True)

matrix = matrix.pivot(index='a', columns='b', values='names').fillna(0)

##################################################
# Reindex with this
matrix = matrix.reindex(index=a_bins.categories,
                        columns=b_bins.categories,
                        fill_value=0)
##################################################

你的代码看起来不错。怎么了?你的代码看起来不错。有什么问题吗?