在Python Pandas中查找大于另一列值的列的最小值

时间:2018-04-04 07:34:32

标签: python pandas

我在Python工作。我有两个数据帧df1和df2:

items = []
items[0] = {id: "805", category: 'Apple'}
items[1] = {id: "804", category: 'Watermelon'}
items[2] = {id: "804", category: 'Plum'}


items.forEach(function(item) {
  console.log(item.category);
});

我想在df1中创建一个新列,其最小时间戳df2的值大于当前df1时间戳,其中df2 [' col03']为零。这就是我这样做的方式:

d1 = {'timestamp1': [88148  , 5617900, 5622548, 5645748, 6603950, 6666502], 'col01': [1, 2, 3, 4, 5, 6]}
df1 = pd.DataFrame(d1)

d2 = {'timestamp2': [5629500, 5643050, 6578800, 6583150, 6611350], 'col02': [7, 8, 9, 10, 11], 'col03': [0, 1, 0, 0, 1]}
df2 = pd.DataFrame(d2)

它有效,但我不想使用for循环。有更好的方法吗?

2 个答案:

答案 0 :(得分:0)

尝试使用def func(x): values = df2['timestamp2'][(df2['timestamp2'] > x) & (df2['col03']==0)] if not values.empty: return values.iloc[0] else: np.NAN df1["timestamp1"].apply(func) 方法。

0    5629500.0
1    5629500.0
2    5629500.0
3    6578800.0
4          NaN
5          NaN
Name: timestamp1, dtype: float64

您可以创建一个单独的函数来执行必须执行的操作。 输出是您的新列

{{1}}

这不是一个单行解决方案,但它有助于保持组织有序。

答案 1 :(得分:0)

使用pd.merge_asof( df1, df2.loc[df2.col03 == 0, ['timestamp2']], left_on='timestamp1', right_on='timestamp2', direction='forward' ).rename(columns=dict(timestamp2='colnew')) col01 timestamp1 colnew 0 1 88148 5629500.0 1 2 5617900 5629500.0 2 3 5622548 5629500.0 3 4 5645748 6578800.0 4 5 6603950 NaN 5 6 6666502 NaN 正向

<script type="text/javascript" src="https://www.google.com/jsapi"></script>   
<script type="text/javascript">

  google.load('visualization', '1.1', {'packages': ['bar']});

google.setOnLoadCallback(drawStuff);

function drawStuff() {
    var data = new google.visualization.DataTable();
    data.addColumn('string', '');
    data.addColumn('number', 'level 1');
    data.addColumn('number', 'level 2');
    data.addColumn('number', 'level 3');
    data.addColumn('number', 'level 4'); 
    data.addColumn('number', 'current value');    
    data.addRows([
        ['Team1',  {!Team1_l1}, {!Team1_l2}, {!Team1_l3}, {!Team1_l4}, {!Team1_cv}],
        ['Team2',  {!Team2_l1}, {!Team2_l2}, {!Team2_l3}, {!Team2_l4}, {!Team2_cv}],
    ]);

    var options = {
        isStacked: true,
        width: 890,
        height: 500,
        backgroundColor: '#F8F8F8',

        chartArea: { backgroundColor: '#F8F8F8' },

        chart: {
            subtitle: 'current view of the incentive'
        },        
        vAxis: {
            format: 'decimal',
            viewWindow: {
                min: 0,
                max: 30000000
            }
        },       
        series: {
            4: { targetAxisIndex: 1 }, 
            5: { targetAxisIndex: 1 }
        }
    };
    var chart = new google.charts.Bar(document.getElementById('chart_div'));
    chart.draw(data, google.charts.Bar.convertOptions(options));
}

</script>

<div id="chart_div"></div>