簡體   English   中英

將 pandas 中的自定義 function output 拆分為多個列

[英]splitting custom function output in pandas to multiple columns

我嘗試尋找類似的答案,但解決方案對我不起作用。

我有一個 dataframe 有兩列:模板(str)和內容(str)。

我還有一個單獨的 function, split_template_name ,它接受一個字符串並返回一個 5 個值的元組,例如:

split_template_name(some_string)將返回一個由 5 個字符串組成的元組('str1', 'str2', 'str3', 'str4', 'str5')

我正在嘗試使用此 function 處理df[template] ,以便 dataframe 獲得 5 個更多列和 5 個輸出。

嘗試df[template].apply(split_template_name)並將完整元組作為一列返回,這不是我需要的。

一些stackoverflow答案建議添加result_type='expand' ,所以我嘗試了df['template'].apply(split_template_name, axis = 1, result_type ='expand')
但這會產生錯誤: split_template_name() got an unexpected keyword argument 'axis'split_template_name() got an unexpected keyword argument 'result_type'

基本上目標是從df['template', 'content']開始並以具有df['template', 'content', 'str1', 'str2', 'str3', 'str4', 'str5']

這似乎有效:

df[['str1', 'str2', 'str3', 'str4', 'str5']] = pd.DataFrame(
    df['template'].apply(split_template_name).tolist(), index = df.index)

如果可以使用正則表達式拆分列,則可以使用:

df.template.str.extract()

看這個例子:

import pandas as pd

df = pd.DataFrame({'sentences': ['how_are_you', 'hello_world_good']})

這個 dataframe 看起來如何:

          sentences
0       how_are_you
1  hello_world_good

使用 Series.str.extract()

df['sentences'].str.extract(r'(?P<first>\w+)_(?P<second>\w+)_(?P<third>\w+)')

output:

   first second third
0    how    are   you
1  hello  world  good

這對我有用。


df_dict = {"template" :["A B C D E","A B C D E","A B C D E","A B C D E","A 
    B C D E"], "content" : ["text1","text2","text3","text4","text5"]}

df = pd.DataFrame(df_dict)

print(df)

    template    content
0   A B C D E   text1
1   A B C D E   text2
2   A B C D E   text3
3   A B C D E   text4
4   A B C D E   text5


def split_template_name(row):
    return row.split()
df[['A','B','C','D','E']] = df['template'].apply(split_template_name)

print(df)


template    content A   B   C   D   E
0   A B C D E   text1   A   A   A   A   A
1   A B C D E   text2   B   B   B   B   B
2   A B C D E   text3   C   C   C   C   C
3   A B C D E   text4   D   D   D   D   D
4   A B C D E   text5   E   E   E   E   E




暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM