简体   繁体   English

在熊猫中做的更好的方法,我有2个循环

[英]A better way to do it in Pandas, I have 2 loops

New to Pandas ; 熊猫新手; is there a better way to do that ? 有更好的方法吗?

import pandas as pd
import numpy as np
from StringIO import StringIO

devices = StringIO("""name;date;CPU;Freq;Voltage
RPI;201501;arm;700MHz;5V
Galileo;201501;intel;400MHz;3.3V
UNO;201502;atmel;16MHz;5V
""")
d = pd.DataFrame.from_csv(devices, sep=';', index_col=None)

comments = StringIO("""comment;t1;t2;t3
cool;arm;;
great!;atmel;;
great!;intel;5V;
fun;atmel;16MHz;
fun;700MHz;atmel;
""")
c = pd.DataFrame.from_csv(comments, sep=';', index_col=None)

n = d.copy()
n['cool'], n['great!'], n['fun'] = 0, 0, 0

for i, row in n.iterrows():
    for j, com in c.iterrows():
        if np.all(np.in1d(np.array(com[['t1', 't2', 't3']].dropna()), np.array(row))):
            n.loc[i, c.loc[j, 'comment']] = 1 

At the end, I build that new DataFrame n, and it looks like that: 最后,我构建了新的DataFrame n,它看起来像这样:

    name    date    CPU     Freq    Voltage     cool    great!  fun
0   RPI     201501  arm     700MHz  5V          1       0       0
1   Galileo 201501  intel   400MHz  3.3V        0       0       0
2   UNO     201502  atmel   16MHz   5V          0       1       1

The other df, d and c look like that 另一个df,d和c看起来像这样

    name    date    CPU     Freq    Voltage
0   RPI     201501  arm     700MHz  5V
1   Galileo 201501  intel   400MHz  3.3V
2   UNO     201502  atmel   16MHz   5V

    comment     t1      t2      t3
0   cool        arm     NaN     NaN
1   great!      atmel   NaN     NaN
2   great!      intel   5V      NaN
3   fun         atmel   16MHz   NaN
4   fun         700MHz  atmel   NaN

I have to use 2 loops to do it. 我必须使用2个循环来做到这一点。 That breaks my dream of Pandas! 那打破了我对熊猫的梦想! Anything better? 有什么更好的吗? must be missing something.. 必须缺少一些东西..

c['val'] = 1

comments = pd.pivot_table(c,index='t1',columns='comment',
                            values='val',aggfunc=sum).fillna(0)

df = pd.merge(d,comments,left_on='CPU',right_index=True,how='left')

comments: 评论:

comment  cool  fun  great!
t1                        
700MHz      0    1       0
arm         1    0       0
atmel       0    1       1
intel       0    0       1

df: DF:

      name    date    CPU    Freq Voltage  cool  fun  great!
0      RPI  201501    arm  700MHz      5V     1    0       0
1  Galileo  201501  intel  400MHz    3.3V     0    0       1
2      UNO  201502  atmel   16MHz      5V     0    1       1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM