[英]pandas apply and assign to multiple columns
I have a dataframe with column like:我有一个 dataframe ,其列如下:
col
a2_3
f4_4
c4_1
I want to add two columns from this column, like so:我想从该列中添加两列,如下所示:
col col1 col2 col3
a2_3 a 2 3
f4_4 f 4 4
c4_1 c 4 1
The following does not work:以下不起作用:
df[['col1', 'col2', 'col3']] = df['col'].apply(lambda s: (s[1], *s[1:].split("_")) )
How can I assign series of tuples to new columns?如何将一系列元组分配给新列?
Here apply
is not necessary, first indexing with str
and then use Series.str.split
with expand=True
:这里apply
不是必需的,首先使用str
进行索引,然后使用Series.str.split
和expand=True
:
df[['col1', 'col2']] = df['col'].str[1:].str.split("_", expand=True)
print (df)
col col1 col2
0 a2_3 2 3
1 f4_4 4 4
2 c4_1 4 1
Your solution is possible with Series
constructor, but it is slow:您的解决方案可以使用Series
构造函数,但速度很慢:
df[['col1', 'col2']] = df['col'].apply(lambda s: pd.Series(s[1:].split("_")))
Faster is use DataFrame constructor:更快的是使用 DataFrame 构造函数:
df1 = pd.DataFrame(df['col'].apply(lambda s: s[1:].split("_")).tolist(), index=df.index)
df[['col1', 'col2']] = df1
Or list comprehension:或列表理解:
df[['col1', 'col2']] = pd.DataFrame([s[1:].split("_") for s in df['col']], index=df.index)
EDIT: Solution is similar:编辑:解决方案类似:
L = df['col'].apply(lambda s: (s[0], *s[1:].split("_"))).tolist()
df[['col1', 'col2', 'col3']] = pd.DataFrame(L, index=df.index)
df[['col1', 'col2', 'col3']] = pd.DataFrame([(s[0], *s[1:].split("_")) for s in df['col']],
index=df.index)
print (df)
col col1 col2 col3
0 a2_3 a 2 3
1 f4_4 f 4 4
2 c4_1 c 4 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.