iT邦幫忙

0

Pandas 指定條件修改內容

  • 分享至 

  • xImage

在Pandas中我想要修改
將斜體的部分改為2019
條件想要用 month = 'Nov' or month = 'Dec'
取代原本的資料集
不知道各位大大是否有較好的寫法呢

先謝謝各位了

year month day time cl SessionID user user_id
2020 Nov 30 19:16:22 cl ftp[4504]: USER sc
2020 Nov 30 19:16:22 cl ftp[4509]: USER sc
2020 Nov 30 19:16:26 cl ftp[4551]: USER sc
2020 Nov 30 19:16:36 cl ftp[4597]: USER ace
2020 Nov 30 19:16:39 cl ftp[4611]: USER yA
2020 Dec 31 23:59:01 cl ftp[11667]: USER sc
2020 Dec 31 23:59:01 cl ftp[11670]: USER nx
2020 Dec 31 23:59:02 cl ftp[11674]: USER sc
2020 Dec 31 23:59:03 cl ftp[11675]: USER sc
2020 Dec 31 23:59:03 cl ftp[11678]: USER sc
2020 Dec 31 23:59:07 cl ftp[11683]: USER sc
2020 Jan 1 00:00:01 cl ftp[12183]: USER sc
2020 Jan 1 00:00:01 cl ftp[12185]: USER ap
2020 Jan 1 00:00:02 cl ftp[12196]: USER ni

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

4
froce
iT邦大師 1 級 ‧ 2020-03-10 15:15:46
最佳解答

https://pyfiddle.io/fiddle/c069b655-84d6-419d-9597-c5a019084841/?i=true

import pandas as pd

data = [
    ["2020", "Nov", "30 19:16:22 cl ftp[4504]: USER sc"],
    ["2020", "Dec", "31 19:16:22 cl ftp[4504]: USER sc"],
    ["2020", "Jan", "30 19:16:22 cl ftp[4504]: USER sc"],
]

data = pd.DataFrame(data, columns=['year', 'month', 'whatever'])

year = data[data['month'].isin(['Nov', 'Dec'])]
year.year = '2019'
data.update(year)

print(data)

我要發表回答

立即登入回答