亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在數據框中拆分單元格

在數據框中拆分單元格

慕村225694 2023-04-18 17:25:26
我有一個 CSV,我將其讀入數據框以刪除某些列并進行一些操作。一些示例行是:20        2/5/1954 13:55          0.5           1821        2/5/1954 14:35          0.5         18.222        2/5/1954 16:35          0.5         18.5我想刪除日期時間中的時間,例如,我得到的2/5/1954不是2/5/1954 13:55.我寫了這個腳本:import pandas as pdfrom datetime import datetime as dtdf = pd.read_csv('habsos_20200310.csv', sep=',', error_bad_lines=False, index_col=False, dtype='unicode')pd.set_option('display.max_rows', None)# Get only the columns we care aboutdfSub = df[['sample_date','sample_depth','water_temp']]# Remove the NaN valuesdfClean = dfSub.dropna()# Select 0.5 depth measurements onlydfClean2 = dfClean.loc[df['sample_depth'] == '0.5']print(dfClean2)這給了我:             sample_date sample_depth   water_temp20        2/5/1954 13:55          0.5           1821        2/5/1954 14:35          0.5         18.222        2/5/1954 16:35          0.5         18.523        2/5/1954 16:52          0.5         18.524        2/5/1954 17:10          0.5         18.625        2/5/1954 17:25          0.5         18.826        2/5/1954 17:43          0.5           19我試圖將這些行添加到我的腳本中以轉換該sample_date列:new_df = dfClean2['sample_date'].str.split()[0]print(new_df)
查看完整描述

1 回答

?
Helenr

TA貢獻1780條經驗 獲得超4個贊

評論已經建議您使用expand=True.?另一種選擇是

dfClean2.sample_date?=?dfClean2.sample_date.str.split('?').str.get(0)

但是,pandas 為 dtype 實現了許多方法datetimeparse_dates=True我建議您傳遞參數.read_csv()(使用 read_csv 處理日期時間)并.dt在該列上使用系列訪問器。

dfClean2.sample_date?=?dfClean2.sample_date.dt.date


查看完整回答
反對 回復 2023-04-18
  • 1 回答
  • 0 關注
  • 109 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號