Python pandas:根据另一个数据帧的类别值创建新列

时间:2016-08-05 08:10:46

标签: python pandas dataframe categories

我有两个数据帧:

  • dfA,其中包含数千行温度数据。每个温度值都链接到从不同对象测量的timeID值(1,2,3,...,n),以便有重复的时间ID
  • dfB包含标识每个时间ID的标签。这些标签是正确的日期/时间(date)值

现在,我想在dfA中创建一个新列,其中包含与右date对应的正确timeID值。我怎样才能做到这一点?

以下是我拥有的几行数据集,例如:

dfA = pd.DataFrame({'timeID': ['1', '2', '3','2','3','4'], 'temp': ['4.5', '5.1', '4.0','-2.3','3.9','-1.1']})
dfB = pd.DataFrame(pd.date_range('6/24/2013', periods=6, freq='10Min'))
seq = pd.Series(range(1, 7)).to_frame()
dfB = pd.concat([seq,dfB],axis=1)
dfB.columns = ['timeID','date']
dfB.set_index('timeID',inplace=True)
print(dfA)
print(dfB)

dfA的输出是:

|    temp  timeID
+-----------------
| 0   4.5      1
| 1   5.1      2
| 2   4.0      3
| 3  -2.3      2
| 4   3.9      3
| 5  -1.1      4

dfB的输出是:

|                      date
| timeID                    
+----------------------------
| 1      2013-06-24 00:00:00
| 2      2013-06-24 00:10:00
| 3      2013-06-24 00:20:00
| 4      2013-06-24 00:30:00
| 5      2013-06-24 00:40:00
| 6      2013-06-24 00:50:00

2 个答案:

答案 0 :(得分:1)

首先,您需要确保timeID列在两个DF中具有相同的dtype,然后您可以使用map()方法:

In [78]: dfA['date'] = dfA['timeID'].astype(dfB.index.dtype).map(dfB['date'])

In [79]: dfA
Out[79]:
   temp timeID                date
0   4.5      1 2013-06-24 00:00:00
1   5.1      2 2013-06-24 00:10:00
2   4.0      3 2013-06-24 00:20:00
3  -2.3      2 2013-06-24 00:10:00
4   3.9      3 2013-06-24 00:20:00
5  -1.1      4 2013-06-24 00:30:00

在较小的DF中转换timeID dtype也是有意义的,因为它会更快(更有效),所以如果dfB更小,我会这样做:

In [82]: dfB.index = dfB.index.astype(str)

In [84]: dfA['date'] = dfA['timeID'].map(dfB['date'])

In [85]: dfA
Out[85]:
   temp timeID                date
0   4.5      1 2013-06-24 00:00:00
1   5.1      2 2013-06-24 00:10:00
2   4.0      3 2013-06-24 00:20:00
3  -2.3      2 2013-06-24 00:10:00
4   3.9      3 2013-06-24 00:20:00
5  -1.1      4 2013-06-24 00:30:00

答案 1 :(得分:0)

试试这个:

<nav>
<div id="cssmenu">
<?php 
get_template_part( 'inc/partials/content', 'header-social');
?>
</div>
<div id="cssmenu">
                    <?php
                    global $wp_customize;
                    if ( !empty( $wp_customize ) && $wp_customize->is_preview() && !get_theme_mod( 'louis_content_set', false ) ) {
                        ?>
                        <ul>
                            <li id="menu-item-16"
                                class="menu-item">
                                <a href="#">Home</a></li>
                            <li id="menu-item-17"
                                class="menu-item">
                                <a href="http://wplift.com/themes/">More Free Themes</a></li>
                            <li id="menu-item-21"
                                class="menu-item">
                                <a href="#">Blog</a></li>
                            <li id="menu-item-22"
                                class="menu-item">
                                <a href="#">Contact</a></li>
                            <li id="menu-item-23"
                                class="menu-item">
                                <a href="#">Members</a></li>
                            <li id="menu-item-24"
                                class="menu-item">
                                <a href="#">Signup</a></li>
                        </ul>
                    <?php
                    } else {
                        wp_nav_menu( array(
                                'theme_location' => 'primary',
                                'container'      => false,
                                'items_wrap'     => '<ul>%3$s</ul>',
                                'depth'          => 0,
                                'fallback_cb'    => 'louis_fallback_menu',
                            )
                        );
                    }
                    ?>
                </div>
    </nav>