第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

如何有條件地拆分熊貓的列

如何有條件地拆分熊貓的列

湖上湖 2021-09-11 17:58:58
我有一個日志 df,在那個 df 我有列描述。好像。DescriptionMachine x : Turn offAnother action hereAnother action hereMachine y : Turn offMachine x : Turn onAnother action here我只需要用“:”分割行喜歡:Description               Machine           ActionMachine x : Turn off      Machine x         Turn offAnother action hereAnother action hereMachine y : Turn off      Machine y         Turn offMachine x : Turn on       Machine x         Turn onAnother action here我已經(jīng)嘗試過:s = df["Description"].apply(lambda x:x.split(":"))df["Action"] = s.apply(lambda x: x[1])df["Machine"] = s.apply(lambda x: x[0])還有一些帶有“startswith”的東西。
查看完整描述

3 回答

?
qq_花開花謝_0

TA貢獻1835條經(jīng)驗 獲得超7個贊

您可以使用str.extract合適的regex. 這將找到周圍的所有值:(也去除冒號周圍的空格):


df[['Machine', 'Action']] = df.Description.str.extract('(.*) : (.*)',expand=True)


>>> df

            Description    Machine    Action

0  Machine x : Turn off  Machine x  Turn off

1   Another action here        NaN       NaN

2   Another action here        NaN       NaN

3  Machine y : Turn off  Machine y  Turn off

4   Machine x : Turn on  Machine x   Turn on

5   Another action here        NaN       NaN


# df[['Machine', 'Action']] = df.Description.str.extract('(.*) : (.*)',expand=True).fillna('')



查看完整回答
反對 回復 2021-09-11
?
婷婷同學_

TA貢獻1844條經(jīng)驗 獲得超8個贊

只需使用split與expand=True


df[['Machine', 'Action']] =df.Description.str.split(':',expand=True).dropna()

df

            Description     Machine     Action

0  Machine x : Turn off  Machine x    Turn off

1   Another action here         NaN        NaN

2   Another action here         NaN        NaN

3  Machine y : Turn off  Machine y    Turn off

4   Machine x : Turn on  Machine x     Turn on

5   Another action here         NaN        NaN


查看完整回答
反對 回復 2021-09-11
?
陪伴而非守候

TA貢獻1757條經(jīng)驗 獲得超8個贊

給定一個數(shù)據(jù)框


>>> df

            Description

0  Machine x : Turn off

1   Another action here

2   Another action here

3  Machine y : Turn off

4   Machine x : Turn on

5   Another action here

我會通過Series.str.split(splitter, expand=True).


>>> has_colon = df['Description'].str.contains(':')

>>> df[['Machine', 'Action']] = df.loc[has_colon, 'Description'].str.split('\s*:\s*', expand=True)

>>> df

            Description    Machine    Action

0  Machine x : Turn off  Machine x  Turn off

1   Another action here        NaN       NaN

2   Another action here        NaN       NaN

3  Machine y : Turn off  Machine y  Turn off

4   Machine x : Turn on  Machine x   Turn on

5   Another action here        NaN       NaN

如果您更喜歡空字符串,可以NaN通過以下方式替換單元格


>>> df.fillna('')

            Description    Machine    Action

0  Machine x : Turn off  Machine x  Turn off

1   Another action here                     

2   Another action here                     

3  Machine y : Turn off  Machine y  Turn off

4   Machine x : Turn on  Machine x   Turn on

5   Another action here 


查看完整回答
反對 回復 2021-09-11
  • 3 回答
  • 0 關(guān)注
  • 186 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號