群组丢弃一些变量
这是原始数据,我需要所有变量的每一年的平均值。
,
但是当我使用groupby('Year')
命令时,它正在删除所有变量除了“ lnmcap”和“ epu”。
为什么会发生这种情况以及需要做什么?
This is the original data and I need the mean of each year of all the variables.
But when I am using groupby('year')
command, it is dropping all variables except 'lnmcap' and 'epu'.
Why this is happening and what needs to be done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
其他列可能具有数据的对象或字符串类型,而不是整数,因此仅
'inmcap'
和'epu'
具有平均列。使用
ds.dtypes
或简单地ds.info()
检查列中的数据类型它出现为对象/字符串类型,然后使用
此功能可以正常工作
Probably the other columns have object or string type of the data, instead of integer, as a result of which only
'Inmcap'
and'epu'
has got the average column.Use
ds.dtypes
or simplyds.info()
to check the data types of data in the columnsit comes out to be object/string type then use
This could work
您可能需要在获得均值之前将所有数值列转换为浮动
You might want to convert all numerical columns to float before getting their mean, for example
您需要将数字列转换为浮点类型。使用
df.info()
检查各种数据类型。之后,使用
df.info()
再次检查。那些带有“ 1.604809'”对象的列将转换为float 1.604809,有时,该列可能包含一些无法转换为float的“脏”数据。在这种情况下,您可以使用
errors ='Coerce'
的代码以下代码,表示非数字数据变为nan
You will need to convert the numeric columns to float types. Use
df.info()
to check the various data types.After this, use
df.info()
to check again. Those columns with objects like '1.604809' will be converted to float 1.604809Sometimes, the column may contain some "dirty" data that cannot be converted to float. In this case, you could use below code with
errors='coerce'
means non-numeric data becomesNaN