在pandas中是否有惯用的方法为pandas中的一组列中的每个值创建行?如下所示?
import pandas as pd
mydf = pd.DataFrame({
'A': ['A1','A2','A3'],
'B': ['B1','B2','B3'],
'C': ['C1','C2','C3'],
'M1': [1,2,3],
'M2': [4,5,6]})
def reshape(dataframe, index_columns, index_colname):
attributes = [c for c in dataframe.columns if c not in index_columns]
dfs_out = []
for c in index_columns:
proj = [a for l in [[c], attributes] for a in l]
tdf = dataframe[proj]
proj[0] = index_colname
tdf.columns = proj
dfs_out.append(tdf)
return pd.concat(dfs_out, ignore_index=True)
print(reshape(mydf, ['A', 'B', 'C'], 'I'))
输出以下内容:
I M1 M2
0 A1 1 4
1 A2 2 5
2 A3 3 6
3 B1 1 4
4 B2 2 5
5 B3 3 6
6 C1 1 4
7 C2 2 5
8 C3 3 6
答案 0 :(得分:5)
您还可以使用pd.lreshape()
将宽格式数据重新整形为长groups
关键字参数接受字典,例如groups ={new_name:columns_to_combine}
pd.lreshape(mydf, dict(I=list("ABC")))
答案 1 :(得分:2)
使用melt
的解决方案,然后需要删除列variable
:
print (pd.melt(mydf, id_vars=['M1','M2'], value_name='I').drop('variable', axis=1))
M1 M2 I
0 1 4 A1
1 2 5 A2
2 3 6 A3
3 1 4 B1
4 2 5 B2
5 3 6 B3
6 1 4 C1
7 2 5 C2
8 3 6 C3