Excel file have data available but i need modify the date using python

Question:

Answers:

I hope, it works for your solution

# install pandas library
# if not pip install pandas in your command line
import pandas as pd
# read excel file
df = pd.read_excel('./excel_file_modify_data.xlsx')
# if No Subscription Name contains pt-dev create a new column Dev
df['Dev'] = df[df['No Subscription Name'].str.contains('pt-dev')]['Cost']
df['PPR'] = df[df['No Subscription Name'].str.contains('pt-ppr')]['Cost']
df['PROD'] = df[df['No Subscription Name'].str.contains('pt-prod')]['Cost']
# only add 10 % when its null
df.loc[~(df['Dev'].isnull()), 'Dev'] = df[~(df['Dev'].isnull())]['Dev'] + df[~(df['Dev'].isnull())]['Dev'] * 0.1
df.loc[~(df['PPR'].isnull()), 'PPR'] = df[~(df['PPR'].isnull())]['PPR'] + df[~(df['PPR'].isnull())]['PPR'] * 0.1
df.loc[~(df['PROD'].isnull()), 'PROD'] = df[~(df['PROD'].isnull())]['PROD'] + df[~(df['PROD'].isnull())]['PROD'] * 0.1
# fill missing values with empty string and then drop the Cost column
df = df.fillna('').drop('Cost', axis=1)
# write excel file with an updated dataframe
df.to_excel('excel_file_modify_data_new.xlsx', index=False) # hope it works for you
Answered By: Muhammad Ali
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.