Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 创建一个矩阵,其中列中有一组范围,行中有一组范围_Python_Pandas_Range_Pandas Groupby - Fatal编程技术网

Python 创建一个矩阵,其中列中有一组范围,行中有一组范围

Python 创建一个矩阵,其中列中有一组范围,行中有一组范围,python,pandas,range,pandas-groupby,Python,Pandas,Range,Pandas Groupby,我有一个数据框,其中一列“F”的值从0到100,第二列“E”的值从0到500。我想创建一个矩阵,其中频率在“F”和“E”的范围内。例如,我想知道“F”的频率范围是20到30,而“E”的频率范围是400到500 我希望得到的是以下矩阵: 我曾尝试使用pd.cut()和groupby()对范围进行分组,但我不知道如何连接数据 非常感谢您帮助创建熊猫矩阵。您可以使用剪切功能为每列创建“标记/名称”栏位。 在对数据帧进行cat旋转后 df['rows'] = pd.cut(df['F'], 5) d

我有一个数据框,其中一列“F”的值从0到100,第二列“E”的值从0到500。我想创建一个矩阵,其中频率在“F”和“E”的范围内。例如,我想知道“F”的频率范围是20到30,而“E”的频率范围是400到500

我希望得到的是以下矩阵:

我曾尝试使用pd.cut()和groupby()对范围进行分组,但我不知道如何连接数据


非常感谢您帮助创建熊猫矩阵。

您可以使用剪切功能为每列创建“标记/名称”栏位。 在对数据帧进行cat旋转后

df['rows'] = pd.cut(df['F'], 5) 
df['cols'] = pd.cut(df['E'], 5) 
df = df.groupby(['rows', 'cols']).agg('sum').reset_index([0,1], False) # your agg func here
df = df.pivot(columns='cols', index='rows')

这就是我发现的创建矩阵的方法,显然是受到@usher答案的启发。我知道这更复杂,但我想和大家分享。再次感谢@usher

E=df.E
F=df.F

bins_E=pd.cut(E, bins=(max(E)-min(E))/100)
bins_F=pd.cut(F, bins=(max(F)-min(F))/10)

bins_EF=bins_E.to_frame().join(bins_F)
freq_EF=bins_EF.groupby(['E', 'F']).size().reset_index(name="counts")
Mat_FE = freq_EF.pivot(columns='E', index='F')

你能展示一下你的代码和你尝试过的东西吗?这样我们就能帮上忙了。非常感谢@usher!你的回答是对的,我正在做的事情。枢轴功能非常适合创建矩阵!