DataFrames: Two-dimensional size-mutable, potentially heterogeneous tabular data.
import pandas as pd
import numpy as np
np.random.seed(101)
df = pd.DataFrame(np.random.randn(5,4),['A','B','C','D','E'],['W','X','Y','Z'])
print(df)
print("***** 选择特定的cell (B,Y) ******")
print(df.loc['B', 'Y'])
print("**** 选择特定的列 ****")
print(df['W'])
# print(df.W) #最好别用这种方式,很混淆
print(type(df['W']))
print("***** 选择特定的列 *****")
print(df[['W','Z']])
print(df[['X','Z']])
print("***** 选择特定的行 ******")
print(df.loc['A'])
print(df.iloc[0])
print(df.loc[['A','C']])
print('\n')
print(df.iloc[[0,2]])
print("****** 选择特定的行和列 ******")
print(df.loc[['A','C'],['W','Y']])
print("****** 创建新的列 ******")
df['new'] = df['W'] + df['Y']
print(df)
new_data = np.random.randn(5,1)
df.insert(5,"new_data", new_data)
print(df)
print("****** 删除列 *******")
print(df.drop('new',axis=1)) #因为shape是个tuple, 根据index来表示行列
print("******* 删除行 *********")
print(df.drop('A',axis=0))
结果如下:
如果觉得不错,就点赞或关注或留言~~
谢谢 ~ ~