Python 数据类型函数在数据帧中工作不正常
我试图使用dtype获取dataframe列的数据类型,但是当我在dataframe中添加一个空行时,dtype给了我错误的值Python 数据类型函数在数据帧中工作不正常,python,pandas,dataframe,dtype,Python,Pandas,Dataframe,Dtype,我试图使用dtype获取dataframe列的数据类型,但是当我在dataframe中添加一个空行时,dtype给了我错误的值 when i am adding one empty row then dtype will give wrong value for int --->float,float-->int. Video Title Up Ratings Down Ratings Views User Name Subscribers 0 1 Adelai
when i am adding one empty row then dtype will give wrong value for int --->float,float-->int.
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
示例1:(没有空行数据类型功能工作正常)
示例:(当我在数据帧中添加空行时获得不同的数据类型。)
有谁能建议我如何在不从代码端显式转换列的数据类型的情况下更正dtype函数功能吗?这可能是dtype函数本身的问题。 我建议你找到那些空行
text_empty = df['column name'].str.len() > -1
[df.loc[text_empty].index]
然后通过将这些行传递给pandas来跳过这些行
还可以使用dropna()删除nan值。如果不需要空行,可以通过以下方式删除这些行:
df=df.dropna()
任何其他解决方案。您需要这些空行吗?不需要。然后直接删除它们。如果我的答案对你有帮助,那么你能支持我的答案吗?请提供一个.@juanpa.arrivillaga示例,我不知道如何创建一个最小的可复制示例。
df=df.dropna()