如何解决Dtype 函数在 Pandas 数据框中无法正常工作
我正在尝试使用 dtype 获取数据框列的数据类型,但是当我在数据框中添加空行时,dtype 给了我错误的值。
when i am adding one empty row then dtype will give wrong value for int --->float,float-->int.
example1:(没有空白行 dtype 函数工作正常)
Video Title Up Ratings Down Ratings Views User Name Subscribers
0 Adelaide 1295 1158259 600.5 Bob 25000
1 Brisbane 5905 1857594 1146.4 Tom 30000
2 Darwin 112 120900 1714.7 Dave 15000
3 Hobart 1357 205556 619.5 Sally 15005
4 Sydney 2058 4336374 1214.8 Rick 20000
5 Melbourne 1566 3806092 646.9 Mary 31111
6 Perth 5386 1554769 869.4 Roberta 11000
In [21]: df.dtypes
Out[21]:
Video Title object
Up Ratings int64
Down Ratings int64
Views float64
User Name object
Subscribers int64
dtype: object
示例:(当我在数据框中添加空行时得到不同的 dtype。)
Video Title Up Ratings Down Ratings Views User Name Subscribers
0
1 Adelaide 1295 1158259 600.5 Bob 25000
2 Brisbane 5905 1857594 1146.4 Tom 30000
3 Darwin 112 120900 1714.7 Dave 15000
4
5 Hobart 1357 205556 619.5 Sally 15005
6 Sydney 2058 4336374 1214.8 Rick 20000
7 Melbourne 1566 3806092 646.9 Mary 31111
8 Perth 5386 1554769 869.4 Roberta 11000
In [21]: df.dtypes
Out[21]:
Video Title object
Up Ratings float64
Down Ratings float64
Views int64
User Name object
Subscribers float64
dtype: object
谁能建议我如何在不从代码端显式转换列的数据类型的情况下更正 dtype 函数功能。?
解决方法
这可能是 dtype 函数本身的问题。 我建议你找到那些空行,
text_empty = df['column name'].str.len() > -1
[df.loc[text_empty].index]
然后通过将它们传递给 pandas.read_csv() 来跳过这些行
您也可以使用 dropna() 删除 nan 值
,如果您不需要空行,可以通过以下方式删除这些行:
df=df.dropna()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。