简体   繁体   中英

How to update the existing excel sheet with data frame in python

I have a excel sheet which has column A to H and all column has data excluding column D. Column D is empty I need to upend Column D with the data frame without deleting the existing column data. please help me with this. Data frame look like below.

print(df)

101
201
301
401
501

excel is as shown below 在此处输入图片说明

Let me assume that you have below data.

Calendar Begin,Calendar End,Contract Start,NUM,Geo,PRODUCT,Sub PRODUCT
2017,2017 Q 4,1/1/2012 0:00,,AA,CCC,NAM
2017,2017 Q 4,1/1/2012 0:00,,AA,CCC,NAM
2017,2017 Q 4,1/1/2012 0:00,,AA,CCC,NAM
2017,2017 Q 4,1/1/2012 0:00,,AA,CCC,NAM
2017,2017 Q 4,1/1/2012 0:00,,AA,CCC,NAM
2017,2017 Q 4,1/1/2012 0:00,,AA,CCC,NAM
2017,2017 Q 4,1/1/2012 0:00,,AA,CCC,NAM
2017,2017 Q 4,1/1/2012 0:00,,AA,CCC,NAM

You can fill NUM column with below code.

import pandas as pd

df_src = pd.read_csv("src.csv")
# print(df_src)

df = pd.DataFrame([
    101,
    201,
    301,
    401,
    501,
])
# print(df)

df_src["NUM"] = df
# print(df_src)

# df_src:
#    Calendar Begin Calendar End Contract Start    NUM Geo PRODUCT Sub PRODUCT
# 0            2017     2017 Q 4  1/1/2012 0:00  101.0  AA     CCC         NAM
# 1            2017     2017 Q 4  1/1/2012 0:00  201.0  AA     CCC         NAM
# 2            2017     2017 Q 4  1/1/2012 0:00  301.0  AA     CCC         NAM
# 3            2017     2017 Q 4  1/1/2012 0:00  401.0  AA     CCC         NAM
# 4            2017     2017 Q 4  1/1/2012 0:00  501.0  AA     CCC         NAM
# 5            2017     2017 Q 4  1/1/2012 0:00    NaN  AA     CCC         NAM
# 6            2017     2017 Q 4  1/1/2012 0:00    NaN  AA     CCC         NAM
# 7            2017     2017 Q 4  1/1/2012 0:00    NaN  AA     CCC         NAM

df_src.to_csv("out.csv")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM