2016-12-16 41 views
0

過去幾天裏一直在吃我的問題。我無法在SO或任何地方找到解決方案。請記住,我仍然在我的Python學習過程中。 我想要做的是從熊貓數據框的2列中刪除尾隨'.0'。嘗試從熊貓數據框打印整數時去除尾部.00

engine = sqlalchemy.create_engine(url, client_encoding='utf8') 
def user_history_summary(userid=198): 
connection = engine.connect()  
start_date = datetime.datetime(2016,8,6) 
end_date = start_date+ datetime.timedelta(days=14) 
last_date=datetime.datetime.now() 
result = connection.execute(text(           
    "SELECT u.id as userid,CASE WHEN h.receiver_user_id = u.id AND h.sender_user_id IS NOT NULL THEN 'Received' WHEN h.sender_user_id=u.id THEN 'Given' ELSE NULL END AS Type, h.sentiment as sentiment, h.context as context,'{0}' as time_period,COUNT(*) as value" 
    " FROM \"User\" u, \"HoorahTransaction\" h" 
    " WHERE (u.id= h.receiver_user_id OR u.id=h.sender_user_id) AND sentiment in ('+','-') AND h.created>'{0}' AND h.created<'{1}'" 
    " group by userid,type,sentiment,context".format(start_date,end_date))) 
answer= result.fetchall() 
totalReceived= pd.DataFrame(answer,columns=["userId","Type","Sentiment","Context","TimePeriod","Value"]) 
counter=0 
while start_date<last_date: 
    counter = counter + 1 
    start_date = start_date+ datetime.timedelta(days=14) 
    end_date = end_date+ datetime.timedelta(days=14) 
    result = connection.execute(text(           
    "SELECT u.id as userid,CASE WHEN h.receiver_user_id = u.id AND h.sender_user_id IS NOT NULL THEN 'Received' WHEN h.sender_user_id=u.id THEN 'Given' ELSE NULL END AS Type, h.sentiment as sentiment, h.context as context,'{0}' as time_period,COUNT(*) as value" 
    " FROM \"User\" u, \"HoorahTransaction\" h" 
    " WHERE (u.id= h.receiver_user_id OR u.id=h.sender_user_id) AND sentiment in ('+','-') AND h.created>'{0}' AND h.created<'{1}'" 
    " group by userid,type,sentiment,context".format(start_date,end_date))) 
    answer= result.fetchall()  
    df=pd.DataFrame(answer,columns=["userId","Type","Sentiment","Context","TimePeriod","Value"]) 
    totalReceived= totalReceived.append(df,ignore_index=True)   
return totalReceived 
totalReceived = user_history_summary() 
print(totalReceived) 

下面是我看到

 userId  Type Sentiment Context   TimePeriod Value 
0  204.0 Received   +  work 2016-08-06 00:00:00 1.0 
1  208.0  Given   +  work 2016-08-06 00:00:00 5.0 
2  220.0 Received   +  work 2016-08-06 00:00:00 3.0 
3  199.0 Received   +  work 2016-08-06 00:00:00 2.0 
4  218.0  Given   +  work 2016-08-06 00:00:00 2.0 
5  199.0  Given   -  work 2016-08-06 00:00:00 1.0 
6  210.0  Given   +  work 2016-08-06 00:00:00 3.0 
7  200.0 Received   +  work 2016-08-06 00:00:00 8.0 
8  207.0  Given   -  work 2016-08-06 00:00:00 1.0 
9  206.0  Given   +  work 2016-08-06 00:00:00 6.0 
10 198.0 Received   +  work 2016-08-06 00:00:00 34.0 
11 212.0  Given   +  work 2016-08-06 00:00:00 1.0 

我需要從‘用戶id’和‘價值’一欄去掉結尾的」 0.0' 的輸出數據幀。數據庫中取值的列都是整數列。

回答

2

您可以將列轉換爲int數據類型。看起來他們目前正在儲存爲float64

for column in ['userId', 'Value']: 
    totalRecieved[column] = totalRecieved[column].astype(int) 
+0

謝謝Andrew ... –