Python 解算ValueError:分解具有不同长度的多个列时,无法从重复轴重新编制索引

Python 解算ValueError:分解具有不同长度的多个列时,无法从重复轴重新编制索引,python,pandas,dataframe,Python,Pandas,Dataframe,我试图解决这一常见问题,这是由于尝试分解多个列时,各列中的列表长度不同造成的: import pandas as pd df = pd.DataFrame({'Student':['J.M.', 'M.G.', 'L.D.'], 'Subject':[['mathematics', 'history', 'literature'], ['physics', 'mathematics', 'geography', 'history'], ['latin', 'literature', 'mathe

我试图解决这一常见问题,这是由于尝试分解多个列时,各列中的列表长度不同造成的:

import pandas as pd
df = pd.DataFrame({'Student':['J.M.', 'M.G.', 'L.D.'], 'Subject':[['mathematics', 'history', 'literature'], ['physics', 'mathematics', 'geography', 'history'], ['latin', 'literature', 'mathematics']], 'Score':[[10, 8, 8.5], [5, 4, 8, 8.5], [4,5, 5]],'Score2':[[10], [5, 4, 8,8.5], [4,5, 5]]})
df = df.apply(pd.Series.explode)

在本例中,第一个
Score
列表有3个元素,第一个
Score2
列表仅1。我试图通过应用一个函数来检查to列表的长度并在较短的列表中附加一个空字符串来解决这个问题。有更好的方法吗?

IIUC,您可以运行爆炸循环:

for col in df:
    df = df.explode(col)
我明白了


假设列表只有1个元素,它映射到其他列中其他列表的第一个元素,我们可以尝试使用
zip\u longest

from itertools import zip_longest

cols = ['Subject', 'Score', 'Score2']
def fun(x):
    return list(zip_longest(*x))

s = pd.Series(map(fun,df[cols].to_records(index=False))).explode()

out = df.drop(cols,1).join(pd.DataFrame(s.tolist(),columns=cols,index=s.index))


当分数有1个元素时,我们可以假设它将映射到其他列中的第一个项目吗?@anky Yesexactly@GM这对我来说没关系:)主题在列列表中,因为它也必须被分解。因为每个学生在一行中都有多个科目,所以你是对的
from itertools import zip_longest

cols = ['Subject', 'Score', 'Score2']
def fun(x):
    return list(zip_longest(*x))

s = pd.Series(map(fun,df[cols].to_records(index=False))).explode()

out = df.drop(cols,1).join(pd.DataFrame(s.tolist(),columns=cols,index=s.index))
print(out)

  Student      Subject  Score  Score2
0    J.M.  mathematics   10.0    10.0
0    J.M.      history    8.0     NaN
0    J.M.   literature    8.5     NaN
1    M.G.      physics    5.0     5.0
1    M.G.  mathematics    4.0     4.0
1    M.G.    geography    8.0     8.0
1    M.G.      history    8.5     8.5
2    L.D.        latin    4.0     4.0
2    L.D.   literature    5.0     5.0
2    L.D.  mathematics    5.0     5.0