Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
296 views
in Technique[技术] by (71.8m points)

python - Dtype function is not working correctly in pandas dataframe

I am trying to get the datatype of the column of dataframe using dtype, but dtype giving me wrong value when I am adding an empty row in the dataframe.

when i am adding one empty row then dtype will give wrong value for int --->float,float-->int.

example1:(without blank row dtype function working fine)

  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

example:(when I am getting a different dtype when I am adding an empty row in dataframe.)

  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

can anyone suggest me how to correct the dtype function functionality without converting datatype of columns explicitly from code end.?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It might be a problem with the dtype function itself. I would suggest you find those empty rows,

text_empty = df['column name'].str.len() > -1
[df.loc[text_empty].index]

and then skip those rows by passing them into pandas.read_csv()

you can also drop nan values with dropna()


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...