Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Filling an Excel file
#1
I have an Excel file ... name, description, scope (column scope is fast empty)
and I want to assign a "code" (0, 1, 2 and 3) in each row of the scope column
when in the first_row 1,2 or all elements of the list l1 are also in the columns name and description
then scope.first_row = 0 and so on.

here is my code but it doesnt work

from pandas import ExcelWriter
import pandas as pd
df2 = pd.read_excel('file.xlsx', 'sheet1', na_values=['NULL'])
#df2

l1 = ["a", "b", "c"]
l2 = ["d", "e", "f"]
l3 = ["g", "h", "i"]
l4 = ["j", "k", "l"]


for ind in df2.index: 
    for i in l1:
        if l1[i] in df2.index and l1[i] in df2.index:
            df2["Scope"] = 0
#df2        
for ind in df2.index: 
    for i in l2:
        if l2[i] in df2.index and l2[i] in df2.index:
            df2["Scope"] = 1
#df2            
for ind in df2.index: 
    for i in l3:
        if l3[i] in df2.index and l3[i] in df2.index:
            df2["Scope"] = 2
            
#df2
for ind in df2.index: 
    for i in l4:
        if l4[i] in df2.index and l4[i] in df2.index:
            df2["Scope"] = 3

#df2    

writer = ExcelWriter('output_file.xlsx')
df2.to_excel(writer,'Sheet1',index=False)
writer.save() 
Can someone help me with this issue ... please
Reply


Messages In This Thread
Filling an Excel file - by starter_student - Jun-27-2019, 02:50 PM
RE: Filling an Excel file - by starter_student - Jun-28-2019, 07:22 AM
RE: Filling an Excel file - by scidam - Jun-29-2019, 10:19 AM
RE: Filling an Excel file - by starter_student - Jul-05-2019, 06:29 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Python openyxl not updating Excel file MrBean12 1 459 Mar-03-2024, 12:16 AM
Last Post: MrBean12
  Copy Paste excel files based on the first letters of the file name Viento 2 572 Feb-07-2024, 12:24 PM
Last Post: Viento
  Search Excel File with a list of values huzzug 4 1,402 Nov-03-2023, 05:35 PM
Last Post: huzzug
  Updating sharepoint excel file odd results cubangt 1 992 Nov-03-2023, 05:13 PM
Last Post: noisefloor
  pandas : problem with conditional filling of a column Xigris 2 704 Jul-22-2023, 11:44 AM
Last Post: Xigris
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,228 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Save and Close Excel File avd88 0 3,369 Feb-20-2023, 07:19 PM
Last Post: avd88
  Trying to access excel file on our sharepoint server but getting errors cubangt 0 898 Feb-16-2023, 08:11 PM
Last Post: cubangt
  Import XML file directly into Excel spreadsheet demdej 0 925 Jan-24-2023, 02:48 PM
Last Post: demdej
  how to read txt file, and write into excel with multiply sheet jacklee26 14 10,801 Jan-21-2023, 06:57 AM
Last Post: jacklee26

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020