I have a 20 x 4000 dataframe in Python using pandas. Two of these columns are named Year
and quarter
. I'd like to create a variable called period
that makes Year = 2000
and quarter= q2
into 2000q2
.
Can anyone help with that?
If both columns are strings, you can concatenate them directly:
df["period"] = df["Year"] + df["quarter"]
If one (or both) of the columns are not string typed, you should convert it (them) first,
df["period"] = df["Year"].astype(str) + df["quarter"]
If you need to join multiple string columns, you can use agg
:
df['period'] = df[['Year', 'quarter', ...]].agg('-'.join, axis=1)
Where "-" is the separator.
这篇关于在 pandas 数据框中合并两列文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!