根据系列输入复制值

时间:2015-07-23 22:41:32

标签: python pandas

我试图编写一个本质上扩展替换功能的自定义函数(具有一些特定于域的关注点)基本上我有一个看起来像的系列:

0   1
1   2
2   4
3   4
4   4

从另一个函数返回值(不幸的是我无法修改它导致它来自另一个源)看起来像是一个类似的系列:

1   A
2   B
4   C

或者看起来像这样的数据框

    cola colb colc
1   A    T    F
2   B    F    F
4   C    T    T

我试图返回看起来像

的输出
0   A
1   B
2   C
3   C
4   C

    cola colb colc
0   A    T    F
1   B    F    F
2   C    T    T
3   C    T    T
4   C    T    T

取决于函数返回的类型。我能够编写一个迭代执行此脚本的脚本,但我觉得必须有一个更高效,更多熊猫特定的方式来执行此操作,所以在我生成一个可怕的嵌套怪物之前,我认为我' d检查是否有良好的支持方式!

2 个答案:

答案 0 :(得分:4)

我不知道您的数据是如何真实的,所以您可能需要稍微修改我的代码,但以下工作使用map

didSet

对于第二个,你可以执行左merge但你必须从你的系列中构建一个数据框:

In [32]:
s.map(s1[1])

Out[32]:
0
0    A
1    B
2    C
3    C
4    C
Name: 1, dtype: object

对于上述内容,您可以在合并后设置索引

修改

实际上,对于您的第二个问题,使用reindex并传递系列值更容易:

In [41]:
pd.DataFrame(s).merge(df, left_on=[1], right_index=True, how='outer')

Out[41]:
   1 cola colb colc
0                  
0  1    A    T    F
1  2    B    F    F
2  4    C    T    T
3  4    C    T    T
4  4    C    T    T

答案 1 :(得分:0)

你可以使用pandas.merge来做到这一点。

package com.tracker.web.models;

import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

@Entity
@Table(name="checklists")
public class Checklist {

    @Id @GeneratedValue(strategy=GenerationType.AUTO)
    private int id;

    private int item_order;
    private String name;
    private String details;
    private String phase;
    private String completed;
    private String skipped_note;
    private Date completed_on;
    private int completed_by;

    @Temporal(TemporalType.TIMESTAMP)
    @CreationTimestamp
    private Date created_at;

    @Temporal(TemporalType.TIMESTAMP)
    @UpdateTimestamp
    private Date updated_at;

    @ManyToOne
    private Event event;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getItem_order() {
        return item_order;
    }

    public void setItem_order(int item_order) {
        this.item_order = item_order;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDetails() {
        return details;
    }

    public void setDetails(String details) {
        this.details = details;
    }

    public String getPhase() {
        return phase;
    }

    public void setPhase(String phase) {
        this.phase = phase;
    }

    public String getCompleted() {
        return completed;
    }

    public void setCompleted(String completed) {
        this.completed = completed;
    }

    public String getSkipped_note() {
        return skipped_note;
    }

    public void setSkipped_note(String skipped_note) {
        this.skipped_note = skipped_note;
    }

    public Date getCompleted_on() {
        return completed_on;
    }

    public void setCompleted_on(Date completed_on) {
        this.completed_on = completed_on;
    }

    public int getCompleted_by() {
        return completed_by;
    }

    public void setCompleted_by(int completed_by) {
        this.completed_by = completed_by;
    }

    public Date getCreated_at() {
        return created_at;
    }

    public void setCreated_at() {
        this.created_at = new Date();
    }

    public Date getUpdated_at() {
        return updated_at;
    }

    public void setUpdated_at() {
        this.updated_at = new Date();
    }

    public Event getEvent() {
        return event;
    }

    public void setEvent(Event event) {
        this.event = event;
    }

}

输出:

s = pd.Series([1,2,4,4,4])
df = pd.DataFrame({'colA':[1,2,4], 'colB':['A','B','C']})
pd.merge(pd.DataFrame(s, columns=['series']), df, left_on='series', right_on='colA')