python dataframe将Int转换为Str时,在末尾添加.0

时间:2018-07-13 09:19:11

标签: python string python-3.x pandas dataframe

打印(数据框)

def model(X, w_h, w_o):
    print_h = tf.nn.sigmoid(tf.matmul(X, w_h)) # this is a basic mlp, think 2 stacked logistic regressions
    h = tf.Print(print_h,[print_h]) # add a node to the execution graph that prints h when ever h is needed and executed in the graph
    return tf.matmul(h, w_o) # note that we dont take the softmax at the end because our cost fn does that for us

上传到Mongo数据库并将其转换为Str时出现错误。

print(data_frame.astype(str))

      Total Price      test_num                                        
0           71.7       2.04256e+14 
1           39.5       2.04254e+14 
2           82.2       2.04188e+14
3           42.9       2.04171e+14

将Int转换为Str时,将在末尾添加.0。 如何有效消除.0? 谢谢

1 个答案:

答案 0 :(得分:2)

int64使用astype

df['test_num'] = df['test_num'].astype('int64')
#alternative
#df['test_num'] = df['test_num'].astype(np.int64)
print (df)
   Total  Price         test_num
0      0   71.7  204256000000000
1      1   39.5  204254000000000
2      2   82.2  204188000000000
3      3   42.9  204171000000000

说明

您可以检查已转换列的dtype-返回float64

print (df['test_num'].dtype)
float64

转换为字符串后,它将删除指数符号并强制转换为float,因此添加了0

print (df['test_num'].astype('str'))
0    204256000000000.0
1    204254000000000.0
2    204188000000000.0
3    204171000000000.0
Name: test_num, dtype: object