import pandas as pd
df = pd.DataFrame({'col1': [1,2,3,4],
'col2': [444,555,666,444],
'col3': ['abc','def','ghi','xyz']})
print(df)
print('\n')
print(df.head())
print('\n')
print('***** 寻找unique number in one column ****')
print(df['col2'].unique())
print('\n')
print('****** length of the array has unique number in one column *****')
print(len(df['col2'].unique()))
print('\n')
print('**** how many times of unique number ****')
print(df['col2'].value_counts())
print('\n')
print('**** 显示某些数字在特定条件下 *****')
print(df['col1'] > 2)
print(df[df['col1'] > 2])
print('\n Used & to do')
print(df[(df['col1']>2) & (df['col2']==444)])
print('\n Used & to do')
print(df[(df['col1']>2) & (df['col2']==44)])
print('\n Used | to do')
print(df[(df['col1']>2) | (df['col2']==444)])
print('\n Used | to do')
print(df[(df['col1']>2) | (df['col2']==44)])
print('\n')
print('*** 结合函数 ***')
def times2(x):
return x * 2
print(df['col1'].apply(times2))
print('\n')
print(df['col3'].apply(len))
print('\n')
print('第二列的数字都乘以2')
print(df['col2'].apply(lambda x:x*2))
print('\n')
print('**** 删除某一列 *****')
print(df.drop('col1',axis=1))
print('\n')
print('返回list of the columns name')
print(df.columns)
print('\n返回index')
print(df.index)
print('\n')
print('***** 排序 *****')
print('递减')
print(df.sort_values('col2', ascending=False))
print('\n递增')
print(df.sort_values(by='col2', ascending=True))
print('\n')
print('*** 判断数据中是否有空 ****')
print(df.isnull())
print('\n')
print('**** 新的数据 ****')
data = {'A': ['foo','foo','foo','bar','bar','bar'],
'B': ['one','one','two','two','one','one'],
'C': ['x','y','x','y','x','y'],
'D': [1,3,2,5,4,1]}
df_new = pd.DataFrame(data)
print(df_new)
print('\n')
print('***** Create a spreadsheet-style pivot table ****')
print(df_new.pivot_table(values='D',index=['A','B'],columns=['C']))
结果如下:
如果觉得不错,就点赞或者关注或者留言~~
谢谢~ ~