Python,如何在excel中添加新的列?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python,如何在excel中添加新的列?相关的知识,希望对你有一定的参考价值。
我有以下文件(file1.xlsx)作为输入。在这个文件中,我总共有32列和近2500行。举个例子,我在屏幕打印中提到了5列
I want to edit same file with python and want output as (file1.xlsx)it should be noted I am adding one column named as short and data is a kind of substring up to first decimal of data present in name(A) column of same excel.
请你请帮助
RegardsKawaljeet
答案
这里是你需要什么...
import pandas as pd
file_name = "file1.xlsx"
df = pd.read_excel(file_name) #Read Excel file as a DataFrame
df['short'] = df['Name'].str.split(".")[0]
df.to_excel("file1.xlsx")
另一答案
大家好,我用下面的代码解决了这个问题。
import pandas as pd
import os
def add_column():
file_name = "cmdb_inuse.xlsx"
os.chmod(file_name, 0o777)
df = pd.read_excel(file_name,) #Read Excel file as a DataFrame
df['short'] = [x.split(".")[0] for x in df['Name']]
df.to_excel("cmdb_inuse.xlsx", index=False)
以上是关于Python,如何在excel中添加新的列?的主要内容,如果未能解决你的问题,请参考以下文章