如何从Python生成的动态循环中删除空值?


11

我有一个这样的数据框:

   ORDER_NO         2401        2504         2600
    2020020         2019-12-04  2019-12-10   2019-12-12 
    2020024         2019-12-25  NaN          2019-12-20
    2020034         NaN         NaN          2019-12-20
    2020020         2019-12-12  2019-12-15   2019-12-18

我从上面的数据框架创建XML。我想删除填充到XML中的空值。我的代码应从XML中删除该特定的列和行值。

我的密码

header = """<ORD>{}</ORD>"""
body ="""
<osi:ORDSTSINF types:STSCDE="{}">
<DTM>{}</DTM>"""

cols = df.columns
for row in df.itertuples():
    with open(f'{row[1]}.xml', 'w') as f:
        f.write(header.format(row[1]))
        for c, r in zip(row[2:], cols[1:]):
            f.write(body.format(r, c))

记录2的电流输出

<ORD>2020024</ORD>
<osi:ORDSTSINF types:STSCDE="2401">
<DTM>2019-12-25</DTM>
<osi:ORDSTSINF types:STSCDE="2504">
<DTM>NaN</DTM>
<osi:ORDSTSINF types:STSCDE="2600">
<DTM>2019-12-20</DTM>

记录2的预期输出

 <ORD>2020024</ORD>
    <osi:ORDSTSINF types:STSCDE="2401">
    <DTM>2019-12-25</DTM>
    <osi:ORDSTSINF types:STSCDE="2600">
    <DTM>2019-12-20</DTM>

如何在Python中完成?

Answers:


6

stack

自然地删除空值

header = """<ORD>{}</ORD>"""
body ="""
<osi:ORDSTSINF types:STSCDE="{}">
<DTM>{}</DTM>"""

for o, d in df.set_index('ORDER_NO').stack().groupby('ORDER_NO'):
    with open(f'{o}.xml', 'w') as f:
        f.write(header.format(o))
        for (o, s), date in d.iteritems():
            f.write(body.format(s, date))

细节

df.set_index('ORDER_NO').stack()

ORDER_NO      
2020020   2401   2019-12-04
          2504   2019-12-10
          2600   2019-12-12
2020024   2401   2019-12-25
          2600   2019-12-20
2020034   2600   2019-12-20
2020020   2401   2019-12-12
          2504   2019-12-15
          2600   2019-12-18

BTW

您的解决方案会很好 if

header = """<ORD>{}</ORD>"""
body ="""
<osi:ORDSTSINF types:STSCDE="{}">
<DTM>{}</DTM>"""

cols = df.columns
for row in df.itertuples():
    with open(f'{row[1]}.xml', 'w') as f:
        f.write(header.format(row[1]))
        for c, r in zip(row[2:], cols[1:]):
            if pd.notna(c):
                f.write(body.format(r, c))

我只是添加了一个页脚(footer = """<END>123</END>""),但输出在同一行<DTM>2020-03-29</DTM><END>123</END>而不是新行中。
Ria Alves

1
当您使用f.write它时,并不假定会'\n'为您放一个。您从body字符串中得到了这些。用footer = """\n<END>123</END>"""
piRSquared
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.