希望修改df:
path.join(app.getPath('userData'),'logs');
所需的输出:
Test_Data = [
('Client', ['A','B','C']),
('2018-11', [10,20,30]),
('2018-12', [10, 20, 30]),
]
df = pd.DataFrame(dict(Test_Data))
print(df)
Client 2018-11 2018-12
0 A 10 10
1 B 20 20
2 C 30 30
因此将现有标题向下移动一行并插入新标题:
Report_Mongo Client Month_1 Month_2
0 Client 2018-11 2018-12
1 A 10 10
2 B 20 20
3 C 30 30
答案 0 :(得分:4)
我建议创建MultiIndex
以便不将数字与DataFrame中的字符串数据混合:
c = ['Report_Mongo','client', 'Month_1', 'Month_2']
#get columns by list without first element
df.columns = [c[1:], df.columns]
#get first element to names of columns
df.columns.names = (c[0], '')
print(df)
Report_Mongo client Month_1 Month_2
Client 2018-11 2018-12
0 A 10 10
1 B 20 20
2 C 30 30
如果需要按行第一行,则可以使用append
,但最可取的是第一个解决方案:
c = ['Report_Mongo','client', 'Month_1', 'Month_2']
df = df.columns.to_frame().T.append(df, ignore_index=True)
df.columns = c[1:]
df.columns.name = c[0]
print(df)
Report_Mongo client Month_1 Month_2
0 Client 2018-11 2018-12
1 A 10 10
2 B 20 20
3 C 30 30