gpt4 book ai didi

Python - Print(df) 只显示第一行

转载 作者:行者123 更新时间:2023-12-05 04:26:45 25 4
gpt4 key购买 nike

我是 python 的初学者。这似乎是一个会被问到的问题,但此时我已经尝试搜索答案 3 天,但找不到。

我在图像上运行 pytesseract 后使用 pd 创建了一个数据框。除了一件“小”事外,一切都很好。当我想让它显示数据框时,如果第一个系列是“日期”,它只显示第一行:

df['Date'] = pd.Series(date_date)
df['In'] = pd.Series(float_in)
df['Out'] = pd.Series(float_out)

df['Date'] = df['Date'].fillna(date_date)
df['Out'] = df['Out'].fillna(0)
df['In'] = df['In'].fillna(0)


print(df)

Date In Out
0 2022-05-31 0.0 7700.0

如果我更改列顺序并将“日期”列保留在任何其他位置,结果很好:

df['In'] = pd.Series(float_in)
df['Out'] = pd.Series(float_out)
df['Date'] = pd.Series(date_date)

df['Date'] = df['Date'].fillna(date_date)
df['Out'] = df['Out'].fillna(0)
df['In'] = df['In'].fillna(0)


print(df)

In Out Date
0 0.0 7700.0 2022-05-31
1 0.0 4232.0 2022-05-31
2 0.0 16056.0 2022-05-31
3 0.0 80000.0 2022-05-31
4 0.0 40000.0 2022-05-31
5 0.0 105805.0 2022-05-31
6 0.0 185500.0 2022-05-31
7 0.0 52188.0 2022-05-31

任何人都可以指导为什么会发生这种情况以及如何解决它?我希望日期保留在第一列,但当然我想要所有行!

提前谢谢你。

如果有帮助,这里是完整的代码:

import cv2
import pytesseract
import pandas as pd
from datetime import datetime

pytesseract.pytesseract.tesseract_cmd=r'C:\Program Files\Tesseract-OCR\tesseract.exe'

img = cv2.imread("C:\\Users\\Fast Computer\\Documents\\Python test\\Images\\page-0.png")
thresh = 255

#Coordinates and ROI for Amount Out
x3,y3,w3,h3 = 577, 495, 172, 815
ROI_3 = img[y3:y3+h3,x3:x3+w3]

#Coordinates and ROI for Amount In
x4,y4,w4,h4 = 754, 495, 175, 815
ROI_4 = img[y4:y4+h4,x4:x4+w4]

#Coordinates and ROI for Date
x5,y5,w5,h5 = 833, 174, 80, 22
ROI_5 = img[y5:y5+h5,x5:x5+w5]


#OCR and convert to strings
text_amount_out = pytesseract.image_to_string(ROI_3)
text_amount_in = pytesseract.image_to_string(ROI_4)
text_date = pytesseract.image_to_string(ROI_5)

text_amount_out = text_amount_out.replace(',', '')
text_amount_in = text_amount_in.replace(',', '')

cv2.waitKey(0)
cv2.destroyAllWindows()

#Convert Strings to Lists
list_amount_out = text_amount_out.split()
list_amount_in = text_amount_in.split()
list_date = text_date.split()

float_out = []
for item in list_amount_out:
float_out.append(float(item))

float_in = []
for item in list_amount_in:
float_in.append(float(item))

date_date = datetime.strptime(text_date, '%d/%m/%Y ')


#Creating columns
df = pd.DataFrame()
df['In'] = pd.Series(float_in)
df['Out'] = pd.Series(float_out)
df['Date'] = pd.Series(date_date)

df['Date'] = df['Date'].fillna(date_date)
df['Out'] = df['Out'].fillna(0)
df['In'] = df['In'].fillna(0)


print(df)

最佳答案

您的问题在于如何初始化然后更新 pd.DataFrame()

import pandas as pd
from datetime import datetime

float_in = [0.0,0.5,1.0]
float_out = [0.0,0.5,1.0,1.5]

# this line just gives you 1 value:
date_date = datetime.strptime('01/01/2022 ', '%d/%m/%Y ')
# date_date = datetime.strptime(text_date, '%d/%m/%Y ')

# creates an empty df
df = pd.DataFrame()

print(df.shape)
# (0, 0)

现在,当您第一次仅使用包含 date_date 的系列填充 df 时,我们得到:

df['Date'] = pd.Series(date_date) # 1 row

print(df.shape)
# (1, 1)

print(df)
# Date
# 0 2022-01-01

向其中添加任何其他(更长的)pd.Series(),不会向 df 添加行。相反,它只会添加该系列的第一个值:

df['In'] = pd.Series(float_in)

print(df)
# Date In
# 0 2022-01-01 0.0

避免这种情况的一种方法是使用延伸最长列表长度的索引初始化 df:

max_length = max(map(len, [float_in, float_out])) # 4

df = pd.DataFrame(index=range(max_length))

print(df.shape)
# (4, 0), so now we start with 4 rows

df['Date'] = pd.Series(date_date)

print(df)
# Date
# 0 2022-01-01
# 1 NaT
# 2 NaT
# 3 NaT

df['In'] = pd.Series(float_in)
df['Out'] = pd.Series(float_out)

df['Date'] = df['Date'].fillna(date_date)
df['Out'] = df['Out'].fillna(0)
df['In'] = df['In'].fillna(0)

print(df)

Date In Out
0 2022-01-01 0.0 0.0
1 2022-01-01 0.5 0.5
2 2022-01-01 1.0 1.0
3 2022-01-01 0.0 1.5

关于Python - Print(df) 只显示第一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72950636/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com