-1

I am having below file(file1.xlsx) as input. In total i am having 32 columns in this file and almost 2500 rows. Just for example i am mentioning 5 columns in screen print

enter image description here

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 upto first decimal of data present in name(A) column of same excel. enter image description here

Request you to please help

Regards Kawaljeet

1
  • Please check this. It can help. Commented Apr 22, 2020 at 9:50

2 Answers 2

0

Here is what you need...

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")
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Soorajit, but I am getting error. To be more precised, i am having 32 columns in total in file and also i don't want to print that sequence number of data frame in excel file. when I am executing this code i am getting error as "ValueError: Length of values does not match length of index"
0

hello guys i solved the problem with below code:

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.