gpt4 book ai didi

python - Pandas 垂直添加单个单元格

转载 作者:行者123 更新时间:2023-12-04 10:44:31 25 4
gpt4 key购买 nike

我需要在列名之前向 df 添加一个单元格(见下图/示例)。可以通过 Pandas 吗?有两个想法,垂直合并/连接/附加两个 df,其中一个只能是单个单元格或 df.loc一个细胞,但没有运气。

df = pd.DataFrame({'Construction Type': [combo2.get()],'Panel Type': [int(upis_tickness)]}, columns['Construction Type', 'Panel Type'])

df_cell = pd.DataFrame([['Panel Schedule - Manufacturing']])

result_df = pd.concat([df, df_cell], axis=1)

Example

最佳答案

关闭,您需要的是MultiIndex :

mux = pd.MultiIndex.from_product([['Panel Schedule - Manufacturing'], 
['Construction Type', 'Panel Type']])
df = (pd.DataFrame({'Construction Type': [combo2.get()],
'Panel Type': [int(upis_tickness)]})
.reindex(columns=mux, level=1))

sample :
mux = pd.MultiIndex.from_product([['Panel Schedule - Manufacturing'], 
['Construction Type', 'Panel Type']])
df = (pd.DataFrame({'Construction Type': [1,2],'Panel Type': [5,9]})
.reindex(columns=mux, level=1))

print (df)

Panel Schedule - Manufacturing
Construction Type Panel Type
0 1 5
1 2 9

编辑:

如果想写入 excel 到第一行,可以使用:
df = pd.DataFrame({'Construction Type': [1,2],'Panel Type': [5,9]})

writer = pd.ExcelWriter('pandas_simple.xlsx', engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1', startrow = 1, index=False)
workbook = writer.book
worksheet = writer.sheets['Sheet1']

text = 'Panel Schedule - Manufacturing'
worksheet.write(0, 0, text)

writer.save()

关于python - Pandas 垂直添加单个单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59769529/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com