簡體   English   中英

在熊貓中做的更好的方法,我有2個循環

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

熊貓新手; 有更好的方法嗎?

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 

最后,我構建了新的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

另一個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

我必須使用2個循環來做到這一點。 那打破了我對熊貓的夢想! 有什么更好的嗎? 必須缺少一些東西..

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')

評論:

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

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