使用Pandas将列表附加到csv中的特定列

时间:2018-03-04 16:20:52

标签: python pandas csv

嗨大家我可以使用Pandas尽可能使用一些帮助:我有一个现有的csv具有以下结构:

column1  column2 column3 column4 
1         ww        1     Sam 
2         ss        2     Bill
                    3     Jack 

list = [[4,Jin],[5,Carl]]

我想写入column3和column4下的csv以获得以下结果:`

column1 column2 column3 column4
 1       ww        1       Sam
 2       ss        2       Bill
                   3       Jack
                   4        Jin
                   5       Carl

对此有何帮助?我知道大熊猫通过行读了怎么做呢?

3 个答案:

答案 0 :(得分:1)

您可以使用pd.DataFrame.append

private void StartReceiving()
{
    Task.Run(() =>
    {
        //using (ImapClient client = new ImapClient("imap.gmail.com", 993, "MY Gmail", "MyPAssword", AuthMethod.Login, true))
        using (ImapClient client = new ImapClient("Outlook.office365.com", 993, "support@mydomain.dk", "MyPAssword", AuthMethod.Login, true))
        {
            if (client.Supports("IDLE") == false)
            {
                MessageBox.Show("Server does not support IMAP IDLE");
                return;
            }

            IEnumerable<uint> uids = client.Search(SearchCondition.Unseen());
            IEnumerable<MailMessage> messages = client.GetMessages(uids);

            client.NewMessage += new EventHandler<IdleMessageEventArgs>(OnNewMessage);
            while (true) ;
        }
    });
}

static void OnNewMessage(object sender, IdleMessageEventArgs e)
{
    MailMessage m = e.Client.GetMessage(e.MessageUID, FetchOptions.Normal);
    f.Invoke((MethodInvoker)delegate
    {
        f.txtMessage.AppendText("-------------------------------------" + DateTime.Now.ToString("dd.MM.yy HH:mm") + " \n " +
            "From: " + m.From + "\n");
        UpdateList(m);
    });
}

答案 1 :(得分:0)

matrix = pd.read_csv(survey_dictionary)#, encoding="UTF-8")
fistElem = None
nullVector = matrix['column3'].isnull()
incr = 0
for isNull in nullVector:
    if isNull == True:
        fistElem = incr
        break
    incr = incr + 1
for source in list:
    if source[0] != fistElem:
        matrix.at[fistElem,'column3'] = source[0]
        matrix.at[fistElem,'column4'] = source[1]
matrix.to_csv(survey_dictionary, index =False, encoding='utf-8-sig')

答案 2 :(得分:0)

我认为这更干净

数据

# Your csv:
import io    
csvstr = """column1;column2;column3;column4
             1;    ww   ;     1 ;   Sam
             2;    ss   ;     2 ;   Bill
              ;         ;     3 ;   Jack"""

inputcsv = io.StringIO(csvstr)
pdcsv = pd.read_csv(inputcsv, delimiter=';')

#   column1 column2  column3 column4
# 0       1      ww        1     Sam
# 1       2      ss        2    Bill
# 2    None    None        3    Jack

要附加的数据

仅使用所需的第3列和第4列来创建要附加的DataFrame行:

lst = [[4,'Jin'],[5,'Carl']]
newrows = pd.DataFrame(lst, columns=pdcsv.columns[2:])
newrows 

# 0      4    Jin
# 1      5    Carl

然后append使用sort=True,可选使用ignore_index=True(如果要删除和重置旧索引)。

pdcsv = pdcsv.append(newrows, sort=True, ignore_index=True)
pdcsv 

#   column1 column2  column3 column4
# 0       1      ww        1     Sam
# 1       2      ss        2    Bill
# 2    None    None        3    Jack
# 3    None    None        4     Jin
# 4    None    None        5    Carl

或与以上相同使用pd.concat

pdcsv = pd.concat([pdcsv, newrows], sort=True, ignore_index=True)