如何通过Pandas在csv中写入多个for循环数据时在单个单元格中追加数据?
How to append data in single cell while writing data of multiple for loop in a csv through Pandas?
问题:-
我在主 for 循环中有两个嵌套循环,用于从 5 页的范围内获取不同的数据。
每页有5行,但是将数据写入csv后,我只得到5个单元格,每个单元格有5个值。
我想要的解决方案 :-
我希望所有的日期和时间都在不同的行中,所以总共应该至少有 25 行。
我的最小代码 :-
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 输出
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 输出
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
我尝试过但没有成功的事情
df = pd.DataFrame({'Date' : [c12],
'Date_time' : [d12]
},
columns=['Date', 'Date_time'])
注意 因为问题只考虑了部分代码,所以我假设之前发生的一切都有效,只进入明显的部分
I want all the dates as well as time to be in different rows
如果两个列表的长度相同,您可以简单地zip()
它们:
pd.DataFrame(zip(c12,d12),columns=['Date', 'Date_time'])
例子
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'])
输出
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.
放置列表以使您的数据保持在循环之外并检查您的缩进。
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(...)
问题:-
我在主 for 循环中有两个嵌套循环,用于从 5 页的范围内获取不同的数据。 每页有5行,但是将数据写入csv后,我只得到5个单元格,每个单元格有5个值。
我想要的解决方案 :-
我希望所有的日期和时间都在不同的行中,所以总共应该至少有 25 行。
我的最小代码 :-
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 输出
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 输出
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
我尝试过但没有成功的事情
df = pd.DataFrame({'Date' : [c12],
'Date_time' : [d12]
},
columns=['Date', 'Date_time'])
注意 因为问题只考虑了部分代码,所以我假设之前发生的一切都有效,只进入明显的部分
I want all the dates as well as time to be in different rows
如果两个列表的长度相同,您可以简单地zip()
它们:
pd.DataFrame(zip(c12,d12),columns=['Date', 'Date_time'])
例子
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'])
输出
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.
放置列表以使您的数据保持在循环之外并检查您的缩进。
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(...)