1 回答
TA贡献1839条经验 获得超15个赞
如果分配不同的索引值,则生成缺失值,如在第一行中。为了正确分配,需要在 和 中使用相同的值。SeriesSeriesDataFrame
问题是返回没有索引值的2d数组:np.c_
print (np.c_[s,t])
[[ 1 2]
[ 2 4]
[ 3 6]
[ 4 8]
[ 5 10]
[ 6 12]]
因此,如果使用使用构造函数创建,则默认从 以下位置开始:DataFrameRange_Index0
df = pd.DataFrame(np.c_[s,t],columns = ["MUL1","MUL2"])
print (df)
MUL1 MUL2
0 1 2 <- first 0 index
1 2 4
2 3 6
3 4 8
4 5 10
5 6 12
print (s)
1 1 <- first 1 index
2 2
3 3
4 4
5 5
6 6
dtype: int64
print (t)
1 2 <- first 1 index
2 4
3 6
4 8
5 10
6 12
dtype: int64
如果更改数据帧构造函数,例如通过字典:
df = pd.DataFrame({"MUL1":s, "MUL2":t})
print (df)
MUL1 MUL2
1 1 2 <- first 1 index
2 2 4
3 3 6
4 4 8
5 5 10
6 6 12
或者将参数添加到构造函数 by 或 Series:indexDataFramest
df = pd.DataFrame(np.c_[s,t],columns = ["MUL1","MUL2"], index=t.index)
print (df)
MUL1 MUL2
1 1 2
2 2 4
3 3 6
4 4 8
5 5 10
6 6 12
因此,如果赋值,例如新列都正常工作,因为在 和 中相同的索引:tdft
df["MUL3"] =t
print (df)
MUL1 MUL2 MUL3
1 1 2 2
2 2 4 4
3 3 6 6
4 4 8 8
5 5 10 10
6 6 12 12
添加回答
举报
