python - 如何迭代熊猫数据框中的行,如果满足条件,则将其打印在适当的 Excel 表中?
问题描述
我有一个数据框,其中一些列是空的。我在 if 语句中有一个标准,如果每行中的某些列为空,则整行从 excel 表中的第 3 行开始转到被拒绝的数据表。Otehrwise,它也将是 Excel 表中从第 3 行开始的已接受数据表中的一行。额外的事情是为每个 excel 工作表中的每个打印行添加边框
目前,整个数据框都打印在每个 Excel 工作表中。
df = df[['Lastname', 'Firstname','Company','Title','Willing_to_share','Willing_to_introduce','work_phones','Work_email','Work_Street','Work_City','Work_State','Work_Zip','Personal_Street','Personal_City','Personal_State','Personal_Zip','mobile_phones','Personal_email','Note','Note_Category']]
##print(df)
## Lastname Firstname Company Title Willing_to_share \
0 Doe Jane
1 Malcoun Joe 8/28/2019 14:29
2 Ramirez Morgan
3 Burki Roman
4 None Jordan
5 None
6 Zachuani Reemo
7 Suarez Geraldo
Willing_to_introduce work_phones Work_email \
0 5678743546 j@greenbriar.com
1 None ceo@nutshell.com
2 3338765438 mramirez@nerdy.com
3 5468756098 burki@bvb.com
4 None jordanw45490@gmail.com
5 None ronny
6 None
7 None
Work_Street Work_City Work_State Work_Zip Personal_Street \
0 54 George street Ridge Springs VA 25678
1 212 South Fifth Ave Ann Arbor MI 48103
2 567 one st Birmingham AL 45678
3 546 fourteen street Nw Dallas TX 54678
4
5
6
7 456 yellow street
Personal_City Personal_State Personal_Zip mobile_phones Personal_email Note \
0 3245687907
1 None
2 6780431874
3 0983457690
4 None
5 None
6 None
7 None
Note_Category
0
1
2
3
4
5
6
7
columns=df.columns
for column, row in df.iterrows():
if (column[0] != ' ' and column[1] != ' ') and ((column[2] != ' '
and column[3] != ' ') or (column[2] == ' ') and (column[6] != ' ')
or (column[16] != ' ') and (column[12] != ' ' and column[13] != '
' and column[14] != ' ' and column[15] != ' ') or (column[8] != '
' and column[9] != ' ' and column[10] != ' ' and column[11] != ' '
and (column[7] != ' ' or column[17] != ' '))):
wb = Workbook()
ws = wb.active
wb2 = Workbook()
ws2 = wb2.active
for r in dataframe_to_rows(df, index=False, header=False):
ws.append(r)
ws2.append(r)
wb.save("Accepted Contacts.xlsx")
wb2.save("Rejected Contacts.xlsx")
else:
wb = Workbook()
ws = wb.active
wb2 = Workbook()
ws2 = wb2.active
for r in dataframe_to_rows(df, index=False, header=False):
ws.append(r)
ws2.append(r)
wb.save("Rejected Contacts.xlsx")
wb2.save("Accepted Contacts.xlsx")
解决方案
推荐阅读
- ios - 尝试在运行 OSX 11.2.2 和 Apple M1 芯片的 MacBook Air 中安装 cocoapods 时出现加载错误
- javascript - Channel.get 未触发
- php - 403错误(Laravel + nginx + centos7)阿里巴巴ecs
- multithreading - 繁重的传奇请求冻结了应用程序
- mysql - Freeradius 3.0 不通过 mysql 发送任何数据(radacct 表为空)
- php - PHP-CPP 返回字符串导致 strlen 而不是纯字符串
- haskell - 是否有帮助为 TH 生成的实例生成正确/非冗余上下文的函数/库/技术?
- json - 将用户数据添加到本地存储只能工作一次,之后添加的对象会被简单地覆盖?(角度 10+)
- https - 通过 ETH 进行 ESP32 OTA
- swift - JWT 令牌解码 SwiftUI