将 numpy 数组转换为 Pandas 数据框 [英] Convert numpy array to pandas dataframe
问题描述
我有一个大小为 31x36
的 numpy 数组,我想转换为 Pandas 数据帧以进行处理.我正在尝试使用以下代码对其进行转换:
I have a numpy array of size 31x36
and i want to transform into pandas dataframe in order to process it. I am trying to convert it using the following code:
pd.DataFrame(data=matrix,
index=np.array(range(1, 31)),
columns=np.array(range(1, 36)))
但是,我收到以下错误:
However, I am receiving the following error:
ValueError: 传递值的形状是 (36, 31),索引意味着 (35, 30)
ValueError: Shape of passed values is (36, 31), indices imply (35, 30)
如何解决问题并正确转换?
How can I solve the issue and transform it properly?
推荐答案
至于你尝试失败的原因,范围相差 1
As to why what you tried failed, the ranges are off by 1
pd.DataFrame(data=matrix,
index=np.array(range(1, 32)),
columns=np.array(range(1, 37)))
因为最后一个值不包括在范围内
As the last value isn't included in the range
实际上看看你在做什么,你本来可以做的:
Actually looking at what you're doing you could've just done:
pd.DataFrame(data=matrix,
index=np.arange(1, 32)),
columns=np.arange(1, 37)))
或者在纯 pandas
中:
pd.DataFrame(data=matrix,
index=pd.RangeIndex(range(1, 32)),
columns=pd.RangeIndex(range(1, 37)))
此外,如果您不指定索引和列参数,则会自动生成索引和列,该索引和列将从 0
开始.不清楚为什么需要它们从 1
Also if you don't specify the index and column params, an auto-generated index and columns is made, which will start from 0
. Unclear why you need them to start from 1
你也可以没有传递索引和列参数,只是在构造后修改它们:
You could also have not passed the index and column params and just modified them after construction:
In[9]:
df = pd.DataFrame(adaption)
df.columns = df.columns+1
df.index = df.index + 1
df
Out[9]:
1 2 3 4 5 6
1 -2.219072 -1.637188 0.497752 -1.486244 1.702908 0.331697
2 -0.586996 0.040052 1.021568 0.783492 -1.263685 -0.192921
3 -0.605922 0.856685 -0.592779 -0.584826 1.196066 0.724332
4 -0.226160 -0.734373 -0.849138 0.776883 -0.160852 0.403073
5 -0.081573 -1.805827 -0.755215 -0.324553 -0.150827 -0.102148
这篇关于将 numpy 数组转换为 Pandas 数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!