Pandas_关于dataFrame.mean()方法的用法与numpy.array()的使用问题

如果dataFrame是用numpy.array()得到的对象(ndarray)来初始化,那么可能使得dataFrame.mean()方法无法正常工作:

使用字典来初始化dataFrame对象则可以使mean()正常工作

''' 7 '''
''' df = pd.DataFrame([[1, 2], [4, 5], [7, 8]],
index=['cobra', 'viper', 'sidewinder'],
columns=['max_speed', 'shield']) '''
data_dict={'dataset':['I','I','I','I','I'],'x':[10,8,13,9,11],'y':[8.04,6.95,7.58,8.81,8.33]}
df=pd.DataFrame(data=data_dict)
''' I found that if use numpy.array to make the dataFrame,the mean() method work uncorrectly!'''
# data_lists=[['I','I','I','I','I'],[10,8,13,9,11],[8.04,6.95,7.58,8.81,8.33]]#the common python lists will be priority by line make the parameter for the constractor with specified columns; the numpy.array instance may be is more flexible:https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.html?highlight=dataframe#pandas.DataFrame
# df=pd.DataFrame(data=np.array(data_lists).T,columns=['dataset','x','y'])
# print(df)
df.loc[[2,4],'dataset']="II"
# tmp=df[[2,4],['dataset']]
# print(df)
''' averages: '''
bool_serial_I=df['dataset']=='I'
# print(df['dataset']=='I')
tmp1=df.loc[bool_serial_I]
# print(tmp1.loc[:,['x','y']])
# tmp1=tmp1.loc[:,['x','y']]
# print(tmp1)
print(tmp1.mean(1,skipna=True))

the error result by numpy.array()

在这里插入图片描述

the normal result:

在这里插入图片描述

the doc of the dataFrame.mean()

在这里插入图片描述

posted @   xuchaoxin1375  阅读(18)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2023-06-18 LA@AM@向量间的位置关系@垂直@平行@共面判定@混合积
2023-06-18 android studio_constraintLayout可视化编辑器的基本使用(Button/textView)
2021-06-18 CN_奇偶校验_奇校验码和偶校验码的概念和实例
点击右上角即可分享
微信分享提示