由于 s 和 t 的索引相同,为什么 t 返回 NaN 作为第一个值?
Since the indexes are same for both s and t, why is t returning NaN as the first value?
系列 t 的第一个值是 NaN,但系列 s 不是。为什么即使系列具有相同的索引也是如此。
import numpy as np
import pandas as pd
s = pd.Series([1,2,3,4,5,6],index= [1,2,3,4,5,6])
t = pd.Series([2,4,6,8,10,12],index= [1,2,3,4,5,6])
df = pd.DataFrame(np.c_[s,t],columns = ["MUL1","MUL2"])
df["MUL2"] =t
df
Output:
MUL1 MUL2
0 1 NaN
1 2 2.0
2 3 4.0
3 4 6.0
4 5 8.0
5 6 10.0
如果将 Series
分配给不同的索引值,则会生成缺失值,如第一行。为了正确分配,Series
和 DataFrame
.
中需要相同的值
问题是 np.c_
return 没有索引值的二维数组:
print (np.c_[s,t])
[[ 1 2]
[ 2 4]
[ 3 6]
[ 4 8]
[ 5 10]
[ 6 12]]
因此,如果使用 DataFrame
构造函数,则默认创建 Range_Index
从 0
开始:
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
如果更改 DataFrame 构造函数,例如按字典:
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
或通过s
或t
系列将index
参数添加到DataFrame
构造函数:
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
因此,如果分配 t
,例如新列都工作正常,因为 df
和 t
:
中的索引相同
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
系列 t 的第一个值是 NaN,但系列 s 不是。为什么即使系列具有相同的索引也是如此。
import numpy as np
import pandas as pd
s = pd.Series([1,2,3,4,5,6],index= [1,2,3,4,5,6])
t = pd.Series([2,4,6,8,10,12],index= [1,2,3,4,5,6])
df = pd.DataFrame(np.c_[s,t],columns = ["MUL1","MUL2"])
df["MUL2"] =t
df
Output:
MUL1 MUL2
0 1 NaN
1 2 2.0
2 3 4.0
3 4 6.0
4 5 8.0
5 6 10.0
如果将 Series
分配给不同的索引值,则会生成缺失值,如第一行。为了正确分配,Series
和 DataFrame
.
问题是 np.c_
return 没有索引值的二维数组:
print (np.c_[s,t])
[[ 1 2]
[ 2 4]
[ 3 6]
[ 4 8]
[ 5 10]
[ 6 12]]
因此,如果使用 DataFrame
构造函数,则默认创建 Range_Index
从 0
开始:
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
如果更改 DataFrame 构造函数,例如按字典:
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
或通过s
或t
系列将index
参数添加到DataFrame
构造函数:
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
因此,如果分配 t
,例如新列都工作正常,因为 df
和 t
:
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