Dataframe 有column 和 index ,column也是key。像平常的表格。见下列例子:
df = pd.DataFrame(np.arange(1,16).reshape(3,5),columns=[‘A’,‘B’,‘C’,‘D’,‘E’],index=[‘one’,‘two’,‘three’])
‘’’
print(df)
输出:
A B C D E
one 1 2 3 4 5
two 6 7 8 9 10
three 11 12 13 14 15
‘’’
‘’’
print(df.keys())
输出: Index([‘A’, ‘B’, ‘C’, ‘D’, ‘E’], dtype=‘object’)
print(df[‘D’].keys())
output: Index([‘one’, ‘two’, ‘three’], dtype=‘object’)
print(df[“D”])
输出:
one 4
two 9
three 14
Name: D, dtype: int32
‘’’
dft = df.transpose()
‘’’
print(dft)
one two three
A 1 6 11
B 2 7 12
C 3 8 13
D 4 9 14
E 5 10 15
‘’’
‘’’
print(dft.keys())
输出: Index([‘one’, ‘two’, ‘three’], dtype=‘object’)
‘’’
‘’’
print(dft[‘one’].keys())
output: Index([‘A’, ‘B’, ‘C’, ‘D’, ‘E’], dtype=‘object’)
‘’’
‘’’
print(dft[‘one’])
output:
A 1
B 2
C 3
D 4
E 5
Name: one, dtype: int32
‘’’
‘’’
X = df - dft[‘one’]
print(X)
A B C D E
one 0 0 0 0 0
two 5 5 5 5 5
three 10 10 10 10 10
‘’’
s = pd.Series([1,2,3,4,5])
Y = df - s
‘’’
print(Y)
output:
A B C D E 0 1 2 3 4
one NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
two NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
three NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
‘’’
s.index=[‘A’,‘B’,‘C’,‘D’,‘E’]
Y = df - s
‘’’
print(Y)
output:
A B C D E
one 0 0 0 0 0
two 5 5 5 5 5
three 10 10 10 10 10
‘’’