[英]From list column in pandas, access each string in list to remove the numbers and periods
我有一個 dataframe 有一列,每行都有字符串列表。 但是每個字符串都有我必須刪除的數字和句點。 我無法訪問每行中列表的字符串,這是示例 dataframe:
df['column_name']
output:
['1.one','2.two','3. three','4.four ']
['1.one','2.two','3. three','4.four ','5.five']
['1.one','2.two','3. three']
...
我嘗試如下,我的 output 是:
df4['column_name'].str[0].str.replace('\d+\.','')
output:
one
one
one
...
但我需要這樣的 output:
df4['column_name'].str[0].str.replace('\d+\.','')
output:
'one', 'two', 'three', 'four'
同樣,我必須遍歷 dataframe 的所有行,:(。任何幫助將不勝感激!!!
你可以試試這個,得到字符串類型的列:
df['column_name'].str.join(',').str.replace('\d+\.|[ ]','').str.replace(',',', ')
或者這是獲取類型列表的列:
df['column_name'].str.join(',').str.replace('\d+\.|[ ]','').str.split(',')
Output:
#first solution:
0 one, two, three, four
1 one, two, three, four, five
2 one, two, three
Name: column_name, dtype: object
#second solution:
0 [one, two, three, four]
1 [one, two, three, four, five]
2 [one, two, three]
Name: column_name, dtype: object
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.