我有一个DataFrame,其中包含两个变量male和female,我想创建两个新列,将数据拆分为male和female
Gender_Years[['male', 'female']] = Gender_Years['sex'].str.split(expand=True)
但得到的错误是ValueError: Columns must be same length as key
ValueError: Columns must be same length as key
jtoj6r0c1#
您是否正在寻找pivot?
pivot
out = (df.pivot_table(index='year', columns='sex', values='Total_Suicides') .rename_axis(columns=None).reset_index()) print(out) # Output year female male 0 1985 27115 24825 1 1986 27606 32757 2 1987 28454 35529
输入 Dataframe :
>>> df sex year Total_Suicides 0 female 1985 27115 1 female 1986 27606 2 female 1987 28454 3 male 1985 24825 4 male 1986 32757 5 male 1987 35529
1条答案
按热度按时间jtoj6r0c1#
您是否正在寻找
pivot
?输入 Dataframe :