我有一张这样的单子
sports = [['Sport', 'Country(s)'], ['Foot_ball', 'brazil'], ['Volleyball', 'Argentina', 'India'], ['Rugger', 'New_zealand', ‘South_africa’], ['Cricket', 'India'], ['Carrom', 'Uk', ‘Usa’], ['Chess', 'Uk']]
我想使用上面的列表创建panda数据框,如下所示:
sport Country(s)
Foot_ball brazil
Volleyball Argentina
Volleyball india
Rugger New_zealnd
Rugger South_africa
Criket India
Carrom UK
Carrom Usa
Chess UK
我试着这样做
sport_x = []
for x in sports[1:]:
sport_x.append(x[0])
print(sport_x)
country = []
for y in sports[1:]:
country.append(y[1:])
header = sports[0]
df = pd.DataFrame([sport_x,country], columns = header)
中途,我得到了这个错误 但我犯了个错误
AssertionError: 2 columns passed, passed data had 6 columns
有什么建议,怎么做
类似这样的方法首先“扩展”不规则形状的行,然后对它们进行数据框架化
编辑:另一个使用
.melt()
的解决方案,但这在我看来更难看,顺序也不一样或者,使用^{} 和列表理解:
相关问题 更多 >
编程相关推荐