簡體   English   中英

在 Python 中,如何根據查詢結果在字段內容上具有 elseif 條件的已創建 csv 文件中插入附加字段?

[英]In Python, how do you insert an additional field in an created csv file that has elseif conditions on the field contents based on results of query?

這是從我的查詢中獲取數據並將其打包到 csv 文件中的 python 代碼。

...
    col_headers = [ i[0] for i in cursor.description ]
    rows = [ list(i) for i in cursor.fetchall()] 
    df = pd.DataFrame(rows, columns=col_headers)
    
    df.to_csv("PremiseCPE.csv", index=False)
       
    for row in cursor.fetchall():
        print (row)
...
  

傳入的數據在列中。 我需要添加一個名為“Placemarks”的附加列(#6)。 然后,我需要根據第 3 列(稱為 cpeStatus)中的值在數據庫的每個輸出的新列行中添加值。 以下是我在創建 kml 文件時嘗試的查詢結構類型:

...
    iif (row[4]) = 'Off', (row[6]) = "http://maps.google.com/mapfiles/kml/shapes/forbidden.png"
    ElseIf (row[4]) = 'Active', (row[6]) = "http://maps.google.com/mapfiles/kml/shapes/ranger_station.png"
    ElseIf (row[4]) = 'Ready, (row[6]) = "http://maps.google.com/mapfiles/kml/shapes/mechanic.png"
    ElseIf (row[4]) = 'Alarm', (row[6]) = "http://maps.google.com/mapfiles/kml/shapes/caution.png"
    ElseIf (row[4]) = 'Null', (row[6]) = "http://maps.google.com/mapfiles/kml/shapes/white_bubble.png"
    End If
...

目標是嘗試在 csv 文件級別運行它。

任何人都可以幫忙嗎?

正如@MattDMo 所說,您需要在寫入 CSV 之前在數據框中執行此操作。 另外,我更喜歡字典查找,而if...elif...else在 python 中進行長if...elif...else 最后,我建議使用pd.read_sql來查詢數據庫並創建 df。

import pandas as pd

col_headers = ['col1', 'cols2', 'yada', 'cpeStatus', 'murgatroyd', 'noimagination']

rows = [[1, 2, 3, 'Off', 'is', 42],
        [2, 4, 42, 'Active', 'the', 42],
        [3, 9, 12, 'Ready', 'best', 42],
        [4, 16, 20, 'Off', 'name', 42],
        [5, 25, 30, 'Alarm', 'no', 42],
        [6, 36, 42, 'Null', 'its', 42],
        [7, 49, 56, 'Danger', 'not', 42],]

df = pd.DataFrame(rows, columns=col_headers)

plmks = {'Off': "forbidden.png",
         'Active': "ranger_station.png",
         'Ready': "mechanic.png",
         'Alarm': "caution.png",
         'Null': "white_bubble.png"}

df['Placemarks'] = [plmks.get(st, "headslap.png") for st in df['cpeStatus']]
print(df)
df.to_csv("PremiseCPE.csv", index=False)

產生以下 df:

0     1      2     3       Off         is             42       forbidden.png
1     2      4    42    Active        the             42  ranger_station.png
2     3      9    12     Ready       best             42        mechanic.png
3     4     16    20       Off       name             42       forbidden.png
4     5     25    30     Alarm         no             42         caution.png
5     6     36    42      Null        its             42    white_bubble.png
6     7     49    56    Danger        not             42        headslap.png

和以下CSV:

col1,cols2,yada,cpeStatus,murgatroyd,noimagination,Placemarks
1,2,3,Off,is,42,forbidden.png
2,4,42,Active,the,42,ranger_station.png
3,9,12,Ready,best,42,mechanic.png
4,16,20,Off,name,42,forbidden.png
5,25,30,Alarm,no,42,caution.png
6,36,42,Null,its,42,white_bubble.png
7,49,56,Danger,not,42,headslap.png

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM