Python Pandas: 如何通过数据框在Excel表格中添加附加列 + 格式化Excel表格?

huangapple go评论58阅读模式
英文:

Python Pandas: How to make additional columns in excell sheet through dataframes + formatting excel sheet?

问题

以下是您要翻译的内容:

"The main thing I wanted to see was how I can add additional columns into the excel sheet through use of dataframes. However, I also wanted to include some formatting options such as merging the cells in the top left and aligning the data points. This is what I mean:

Picture 1 (What my code currently outputs):

Python Pandas: 如何通过数据框在Excel表格中添加附加列 + 格式化Excel表格?

Picture 2 (What I want it to output):

Python Pandas: 如何通过数据框在Excel表格中添加附加列 + 格式化Excel表格?

Mainly, I want to be able to dynamically change the number of columns per category. So if I have additional data, the code should be able to create any amount of columns needed as to only 2. Also just formatting so the table will look clean (like the Type of Data Merged Cell and centering the data).

Here's the code for picture 1:

import pandas as pd

# Create a Pandas dataframe from the data.
df1 = pd.DataFrame([['a', 'b'], ['c', 'd']],
                    index=['row 1', 'row 2'],
                    columns=['col 1', 'col 2'])

df2 = pd.DataFrame([['a', 'b'], ['c', 'd']],
                    index=['row 1', 'row 2'],
                    columns=['col 1', 'col 2'])

df3 = pd.DataFrame([['a', 'b'], ['c', 'd']],
                    index=['row 1', 'row 2'],
                    columns=['col 1', 'col 2'])

# Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter('pandas_simple.xlsx', engine='xlsxwriter')

# Concatenate data frames for separate data types
out1 = (
    pd.concat({
        'data type 1': df1,
        'data type 2': pd.DataFrame([[1, 2], [3, 4]], columns=df1.columns, index=df1.index)
    }).swaplevel().sort_index()
)

out2 = (
    pd.concat({
        'data type 1': df2,
        'data type 2': pd.DataFrame([[1, 2], [3, 4]], columns=df2.columns, index=df2.index)
    }).swaplevel().sort_index()
)

out3 = (
    pd.concat({
        'data type 1': df3,
        'data type 2': pd.DataFrame([[1, 2], [3, 4]], columns=df3.columns, index=df3.index)
    }).swaplevel().sort_index()
)

# Convert the dataframe to an XlsxWriter Excel object.
out1.to_excel(writer, sheet_name='Sheet1')
out2.to_excel(writer, sheet_name='Sheet1', startrow=6)
out3.to_excel(writer, sheet_name='Sheet1', startrow=12)

# Close the Pandas Excel writer and output the Excel file.
writer.close()

I'm assuming for extra columns I need to do something similar when I concatenate the index's for additional rows?"

英文:

The main thing I wanted to see was how I can add additional columns into the excel sheet through use of dataframes. However, I also wanted to include some formatting options such as merging the cells in the top left and aligning the data points. This is what I mean:

Picture 1 (What my code currently outputs):

Python Pandas: 如何通过数据框在Excel表格中添加附加列 + 格式化Excel表格?

Picture 2 (What I want it to output):

Python Pandas: 如何通过数据框在Excel表格中添加附加列 + 格式化Excel表格?

Mainly, I want to be able to dynamically change the number of columns per category. So if I have additional data, the code should be able to create any amount of columns needed as to only 2. Also just formatting so the table will look clean (like the Type of Data Merged Cell and centering the data).

Here's the code for picture 1:

import pandas as pd

# Create a Pandas dataframe from the data.
df1 = pd.DataFrame([['a', 'b'], ['c', 'd']],
                    index=['row 1', 'row 2'],
                    columns=['col 1', 'col 2'])

df2 = pd.DataFrame([['a', 'b'], ['c', 'd']],
                    index=['row 1', 'row 2'],
                    columns=['col 1', 'col 2'])

df3 = pd.DataFrame([['a', 'b'], ['c', 'd']],
                    index=['row 1', 'row 2'],
                    columns=['col 1', 'col 2'])

# Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter('pandas_simple.xlsx', engine='xlsxwriter')

# Concatenate data frames for seperate data types
out1 = (
    pd.concat({
        'data type 1': df1,
        'data type 2': pd.DataFrame([[1, 2], [3, 4]], columns=df1.columns, index=df1.index)
    }).swaplevel().sort_index()
)

out2 = (
    pd.concat({
        'data type 1': df2,
        'data type 2': pd.DataFrame([[1, 2], [3, 4]], columns=df2.columns, index=df2.index)
    }).swaplevel().sort_index()
)

out3 = (
    pd.concat({
        'data type 1': df3,
        'data type 2': pd.DataFrame([[1, 2], [3, 4]], columns=df3.columns, index=df3.index)
    }).swaplevel().sort_index()
)

# Convert the dataframe to an XlsxWriter Excel object.
out1.to_excel(writer, sheet_name='Sheet1')
out2.to_excel(writer, sheet_name='Sheet1', startrow=6)
out3.to_excel(writer, sheet_name='Sheet1', startrow=12)

# Close the Pandas Excel writer and output the Excel file.
writer.close()

I'm assuming for extra columns I need to do something similar when I concatenate the index's for additional rows?

答案1

得分: 1

你可以将一个列定义为变量,然后保存到Excel文件中,示例:

df = pd.read_csv('sample_submission.csv')

df['Transported'] = Guess_list

df.to_csv('sample_submission.csv', index=False)

这将保存一个列表到列 'Transported' 中。

如果你想要创建一个新列,可以这样做:

df = pd.read_csv('sample_submission.csv')

df.insert(1, 'Column 2', Guess_list)

df.to_csv('sample_submission.csv', index=False)
英文:

You can define a column as a variable then save it to the excel file, example:

df = pd.read_csv('sample_submission.csv')

df['Transported'] = Guess_list

df.to_csv('sample_submission.csv', index=False)

This saves a list to the column 'Transported'.

If you want to create a new column you could do

df = pd.read_csv('sample_submission.csv')

df.insert(1, 'Column 2', Guess_list)

df.to_csv('sample_submission.csv', index=False)

huangapple
  • 本文由 发表于 2023年4月17日 10:08:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76031293.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定