PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, May 12, 2022

[FIXED] How to append data in single cell while writing data of multiple for loop in a csv through Pandas?

 May 12, 2022     append, file-writing, for-loop, pandas, python     No comments   

Issue

Problem:-

I've two nested loop inside a main for loop fetching different data from a range of 5 pages. Every page has 5 rows, But after writing data to csv, I only get 5 cells in which each cell has 5 values.

Solution I want :-

I want all the dates as well as time to be in different rows, so total there should be at least 25 rows.

My Minimal Code :-

all = []

for i in range(d, 0, -1):
    driver.find_element_by_link_text(f'{i}').click()
    time.sleep(5)
    c12 = []
    d12 = []

    date1 = driver.find_elements_by_class_name('Login_d')
    for i in reversed(date1):
        print(i.text)
        c12.append(i.text)
        if i is None:
            break

    date_time = driver.find_elements_by_class_name('Logout_d')
    for i1 in reversed(date_time):
        print(i1.text)
        d12.append(i1.text)
        if i1 is None:
            break

    z = [c12, d12]
    all.append(z)


 df = pd.DataFrame(all)
 path = 'C:\\Application Data\\pyt_project\\datahub'
 a1 = ['Date', 'Date_time']
 filename = 
 dt.datetime.now().strftime("Login_logout_time_%d_%b_%y_%I_%M_%p.csv")
 p1 = os.path.join(path, filename)
 df.to_csv(p1, headers = a1, index=True)
 print('printed output')

CSV Output I got from above code

                              Date                                        Date_time
 03 Nov 2021 08 Nov 2021 09 Nov 2021 10 Nov 2021 11 Nov 2021 | 18:39 12:59 13:05 12:57 12:57

CSV Output I want

   Date     Date_time
03 Nov 2021  18:39
08 Nov 2021  12:59
09 Nov 2021  13:05
10 Nov 2021  12:57
11 Nov 2021  12:57

Things I tried but didn't worked

df = pd.DataFrame({'Date' : [c12],
                'Date_time' : [d12]
                },
                columns=['Date', 'Date_time'])

Solution

Note Because the question only considers part of the code, I'll assume that everything that happens before works and only go into the obvious parts

I want all the dates as well as time to be in different rows

You can simply zip() your two lists if they have the same length:

pd.DataFrame(zip(c12,d12),columns=['Date', 'Date_time'])

Example

import pandas as pd

c12 = ['03 Nov 2021', '08 Nov 2021','09 Nov 2021','10 Nov 2021','11 Nov 2021']
d12 = ['18:39','12:59','13:05','12:57','12:57']

pd.DataFrame(zip(c12,d12),columns=['Date', 'Date_time'])

Output

Date Date_time
03 Nov 2021 18:39
08 Nov 2021 12:59
09 Nov 2021 13:05
10 Nov 2021 12:57
11 Nov 2021 12:57

But after writing data to csv, I only get 5 cells in which each cell has 5 values.

Put the lists to keep your data outside the loop and check your indentation.

c12 = []
d12 = []

for i in range(d, 0, -1):
    driver.find_element_by_link_text(f'{i}').click()
    time.sleep(5)

    date1 = driver.find_elements_by_class_name('GridBiMonthlyBonusEligibleListDatetd.NoSortColumn')
    for i in reversed(date1):
        print(i.text)
        c12.append(i.text)
        if i is None:
            break

    date_time = driver.find_elements_by_class_name('GridBiMonthlyBonusEligibleListLoginDatetd.NoSortColumn')
    for i1 in reversed(date_time):
        print(i1.text)
        d12.append(i1.text)
        if i1 is None:
            break

pd.DataFrame(zip(c12,d12),columns=['Date', 'Date_time'])#.to_csv(...)


Answered By - HedgeHog
Answer Checked By - Mary Flores (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing