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

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

使用 pandas str.split,其中出現需要是 pandas 列的值

使用 pandas str.split,其中出現需要是 pandas 列的值

侃侃爾雅 2023-06-20 10:24:58
在弄清楚 pandas str.split 時遇到一些麻煩。出現的位置來自列值,而不是為要拆分的字符串放置一個靜態值。我環顧四周尋找類似類型的問題,但大多數似乎只是采用靜態方法來解決問題。下面我有數據框。.str.split('|',1).str[-1] 將在管道 ('|') 第一次出現時刪除字符串的左側部分。這種靜態方法將在整個系列中執行相同的操作。因為 occurrence 參數不會改變。我想要發生的事情: .str.split('|', df['occurrence'] ).str[-1] 可以是動態的并利用出現列中的值并用作 str.split 出現爭論。如果值為零或更小,則不會對字符串采取任何操作。lambda 語句實際上工作并正確執行,但是,它從字符串的右側開始,根據管道之間的值拆分和連接。但最后的結局是好的。不同的方法。我只是不能讓它從字符串的左側做同樣的事情。最后一點:刪除需要從字符串的左邊開始。#-------------------import pandas as pdfrom pandas import DataFrame, Seriesimport numpy as npdata_1 = {'occurrence': [7,2,0,3,4,0],        'string': ['1|2|3|4|5|6|7|8|9|10|11|12','10|11.2|12.2|13.6|14.7','1|2|3',                   '1|2|3|4|5|6|7|8','1|2.4|3|4.6|5|6.2|7|8.1','1|2|3|4|5'] }df = pd.DataFrame(data_1)df['string'] = df['string'].str.split('|',1).str[-1]  # Works but is static only# df['string'] = df['string'].str.split('|',df['occurrence']).str[-1]  # Trying to use occurrence                                                                        # column value as argument# Does work BUT starts with right side of string. Needs to be left.# df['string'] = df.apply(lambda x: '|'.join(x['string'].split('|')[:x.occurrence - 2]), axis=1) print(df)#-------------------Start with:                                        What I would like:occurrence   string                                occurrence    string   7            1|2|3|4|5|6|7|8|9|10|11|12            7             8|9|10|11|122            10|11.2|12.2|13.6|14.7                2             12.2|13.6|14.7 0            1|2|3                                 0             1|2|3 3            1|2|3|4|5|6|7|8                       3             4|5|6|7|8 4            1|2.4|3|4.6|5|6.2|7|8.1               4             5|6.2|7|8.10            1|2|3|4|5                             0             1|2|3|4|5如果您能為我解決這個問題提供有關此主題的任何幫助,我將不勝感激。一如既往,您的時間很寶貴,我為此感謝您。
查看完整描述

3 回答

?
慕村225694

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

用于圍繞 delimiterSeries.str.split拆分列,然后使用列表理解壓縮拆分列并處理值:string|zipoccurence


df['string'] = ['|'.join(s[i:]) for i, s in zip(df['occurrence'], df['string'].str.split('|'))]

結果:


print(df)

   occurrence          string

0           7    8|9|10|11|12

1           2  12.2|13.6|14.7

2           0           1|2|3

3           3       4|5|6|7|8

4           4     5|6.2|7|8.1

5           0       1|2|3|4|5

性能(使用 測量timeit):


df.shape

(60000, 2)


%%timeit -n10

_ = ['|'.join(s[i:]) for i, s in zip(df['occurrence'], df['string'].str.split('|'))]

67.9 ms ± 2.05 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)


%%timeit -n10 (using 'apply')

_ = df.apply(lambda x: '|'.join(x['string'].split('|')[x.occurrence:]), axis=1)

1.93 s ± 34.2 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)


查看完整回答
反對 回復 2023-06-20
?
交互式愛情

TA貢獻1712條經驗 獲得超3個贊

嘗試將您的 lambda 表達式更改為:


df.apply(lambda x: '|'.join(x['string'].split('|')[x.occurrence:]), axis=1)

如果你想要后面的元素(右側),你應該從出現作為索引開始。


結果:


0      8|9|10|11|12

1    12.2|13.6|14.7

2             1|2|3

3         4|5|6|7|8

4       5|6.2|7|8.1

5         1|2|3|4|5


查看完整回答
反對 回復 2023-06-20
?
茅侃侃

TA貢獻1842條經驗 獲得超21個贊

一種有點非正統的方法:從中構建一個正則表達式df['occurrence']并使用它來匹配:


df['regex'] = df['occurrence'].map(lambda o: '^' + r'(?:[^|]*\|)'*o + r'(.*)$')   

df['regex']


0    ^(?:[^|]*\|)(?:[^|]*\|)(?:[^|]*\|)(?:[^|]*\|)(...

1                         ^(?:[^|]*\|)(?:[^|]*\|)(.*)$

2                                               ^(.*)$

3              ^(?:[^|]*\|)(?:[^|]*\|)(?:[^|]*\|)(.*)$

4    ^(?:[^|]*\|)(?:[^|]*\|)(?:[^|]*\|)(?:[^|]*\|)(...

5                                               ^(.*)$

Name: regex, dtype: object

現在只適用re.match于每一行:


df.apply(lambda row: re.match(row['regex'], row['string']).group(1), axis=1) 


0      8|9|10|11|12

1    12.2|13.6|14.7

2             1|2|3

3         4|5|6|7|8

4       5|6.2|7|8.1

5         1|2|3|4|5

dtype: object


查看完整回答
反對 回復 2023-06-20
  • 3 回答
  • 0 關注
  • 164 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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