我在工作中有一个情况,我需要从前一个创建一个新的Dataframe
前一个数据帧看起来像:| |删除id |名称|平衡| minimum_balance | payment_provider_account_id | |型api_user_id | customer_id ||-----|------------|------:|------:|--------------:|---------------------------|--------|-|-||38049|LIDIA CIUBUC|3.58 |-1 |2.0 |LIDIA;ciubuc | 个人|南| 38049 |
新的数据框架将有相同的列,但我需要:检查前一个数据框上的每一行的名称是否已经在新数据框上存在如果该名称不存在,则将该行添加到新数据框架中如果存在,我需要检查前一个的删除状态是否为-1如果不是-1,我需要把之前的数据帧余额加到新的数据帧上如果是-1,我需要在新数据框上临时存储现有的余额,用前一个数据框的行替换该行,然后在新数据框上对替换行的临时余额求和。
目前为止我的代码是:
import sys, psycopg2
import pandas as pd
conn = psycopg2.connect(host="<db_IP>",port=<DB_port>,dbname="dbname",user="postgres",password="Start123")
cur = conn.cursor()
df = pd.read_sql('SELECT * FROM money_account;',conn)
cur.rowcount
conn.commit()
cur.close()
conn.close()
dc = 1
rc = 0
print(df.loc[0])
rows = df.loc[dc-1]
print(rows)
type(rows)
for x in rows.id:
for y in df.id:
if rows.name[rc] == df.names[dc]:
if df.deleted[dc] != -1:
rows.balance[rc] = rows.balance[rc] + df.balance[dc]
else:
temp = rows.balance[rc]
rows[rc] = df[dc]
rows.balance[rc] = rows.balance[rc] + temp
else:
dc = dc +1
rc = rc + 1
print(rows)
但是我收到以下错误:
TypeError Traceback (most recent call last)
<ipython-input-4-316da58d0cb6> in <module>
6 type(rows)
7
----> 8 for x in rows.id:
9 for y in df.id:
10 if rows.name[rc] == df.balance[dc]:
TypeError: 'numpy.int64' object is not iterable
似乎下面的代码为我工作:
import sys, psycopg2
import pandas as pd
import csv
conn = psycopg2.connect(host="127.0.0.1",port=5433,dbname="YPSDB",user="postgres",password="Start123")
cur = conn.cursor()
df = pd.read_sql('SELECT * FROM money_account;',conn)
cur.rowcount
conn.commit()
cur.close()
conn.close()
df_list = df.values.tolist()
df_items=list(df)
for indout in range(len(df_list)-1):
if df_list[indout][6] == 'PERSONAL':
if df_list[indout][3] == -1:
for indin in range(len(df_list)-1):
if df_list[indout][5] == df_list[indin][5]:
if df_list[indin][3] != -1 & bool(df_list[indin][2] != 0.0):
df_list[indout][2] = round(df_list[indout][2] + df_list[indin][2],2)
lista = [sub for sub in df_list if -1 in sub]
with open('final balance.csv', 'w', encoding='utf-8') as f:
write = csv.writer(f)
write.writerow(df_items)
write.writerows(lista)
当然,我愿意听取建议来改进它