lundi 13 février 2017

pandas GroupBy aggregate only one column

Vote count: 0

I have a DataFrame of the following form:

>>> sales = pd.DataFrame({'seller_id':list('AAAABBBB'),'buyer_id':list('CCDECDEF'),\
                          'amount':np.random.randint(10,20,size=(8,))})
>>> sales = sales[['seller_id','buyer_id','amount']]
>>> sales
  seller_id buyer_id  amount
0         A        C      18
1         A        C      15
2         A        D      11
3         A        E      12
4         B        C      16
5         B        D      18
6         B        E      16
7         B        F      19

Now what I would like to do is for each seller calculate the share of total sale amount taken up by its largest buyer. I have code that does this, but I have to keep resetting the index and grouping again, which is wasteful. There has to be a better way. I would like a solution where I can aggregate one column at a time and keep the others grouped. Here's my current code:

>>> gr2 = sales.groupby(['buyer_id','seller_id'])
>>> seller_buyer_level = gr2['amount'].sum() # sum over different purchases
>>> seller_buyer_level_reset = seller_buyer_level.reset_index('buyer_id')
>>> gr3 = seller_buyer_level_reset.groupby(seller_buyer_level_reset.index)
>>> result = gr3['amount'].max() / gr3['amount'].sum()

>>> result
seller_id
A    0.589286
B    0.275362

I simplified a bit. In reality I also have a time period column, and so I want to do this at the seller and time period level, that's why in gr3 I'm grouping by the multi-index (in this example, it appears as a single index). I thought there would be a solution where instead of reducing and regrouping I would be able to aggregate only one index out of the group, leaving the others grouped, but couldn't find it in the documentation or online. Any ideas?

asked 2 mins ago

Let's block ads! (Why?)



pandas GroupBy aggregate only one column

How to design table 1 unique id storing 10 datas

Vote count: -1

I am learning database with mysqli.

I want to know how can I design a table in a way that I can store 10 different datas per unique id but only in 1 table?

is that even possible? or is that the way to do it? should I have 2 different table?

here is what I need

  • id primary key, auto_increment
  • username varchar (30) (should I index this?)
  • wish_list varchar(255)
  • timestamp timestamp

here is what I want it to look like

| id | nickname | wish_list     | timestamp |
| 1  | name1      | wish list1    | 1/3/17       |
|     |                  | wish list2    | 1/3/17       |
|     |                  | wish list10  | 1/4/17       |
| 2  | name2      | wish list1    | 1/4/17       |
|     |                  | wishlist2     | 1/4/17       |

it's a rough sketch but lets say I have a input field on first page to input username and I can input up to 10 data per unique id/nickname on next page.
next time user visits and input username, that user can add something to wishlist.

*forget about security. so anyone can add other users wishlist.
I am doing this because I want to know the process of the database, how it works.

asked 2 mins ago

Let's block ads! (Why?)



How to design table 1 unique id storing 10 datas

Combine array data into one variable

Vote count: 0

I have foreaching array:

$arr = array(2,3,4);
$copy = $arr;
foreach ($arr as $val) {
    echo $val;
    if (next($copy )) {
        echo ','; // Add comma for all elements instead of last
    }
}

and the result is:

2,3,4

How to create a variable with value is above result, so it will looks like $var = '2,3,4' ?

asked 2 mins ago

Let's block ads! (Why?)



Combine array data into one variable

PHP mcrypt concatenate string crypts each string seperatly

Vote count: 0

I have currently started to work with PHP with a Java background and came accross some issues. I am using mcrypt for a basic encryption using mcrypt_encrypt ( string $cipher , string $key , string $data , string $mode [, string $iv ] ) the encryption works successfully but there is a case where I need to concatenate 2 strings and then encrypt them but when I do this the output is just as if I had encrypted each string separately then concatenated them afterwards and not before the encryption. What I am doing is this :

function encryptCode($data){
    return mcrypt_encrypt( MCRYPT_DES , 'myKey' , $data , 'cbc' ,'myIV'); 
}

function decryptCode($data){
    return mcrypt_decrypt( MCRYPT_DES , 'myKey' , $data , 'cbc' ,'myIV'); 
}

$decrypted = decryptCode('somePreviouslyEncryptedString');
$decrypted = $decrypted.'stringToAdd';
$encrypted = encryptCode($decrypted);
print_r($encrypted);

the output is then as if I had encrypted 'stringToAdd' seperately then added it to the previously encrypted string.

I am aware of the weakness of DES but I need it in this case so please no comments about that. Thanks to all for your help.

asked 2 mins ago

Let's block ads! (Why?)



PHP mcrypt concatenate string crypts each string seperatly

How to link a NASM code and GCC in Mac OS X?

Vote count: 0

I'm unable to link my code with gcc, check this code:

extern printf
global main
section .data
  mensaje: db "Hola mundo!", 10,0
section .text
main:
  push mensaje
  call printf
  add esp, 4
ret

The output I get is:

Undefined symbols for architecture i386: "_main", referenced from: implicit entry/start for main executable "printf", referenced from: main in holamundo.o ld: symbol(s) not found for architecture i386 clang: error: linker command failed with exit code 1 (use -v to see invocation)

I am using this command:

nasm -f macho32 holamundo.asm

and gcc command(I get the error, here):

gcc -m32 holamundo.o -o holamundo

Thanks in advance, my english is ever bad.

asked 28 secs ago

Let's block ads! (Why?)



How to link a NASM code and GCC in Mac OS X?

About AlarmManager in Android

Vote count: 0

I know that by using AlarmManager, you can start a service at a specific time, and to register an alarm, you will need to set it in the "onCreate" method of your activity. The problem is, the "onCreate" method will be called every time the application is open, and thus the alarm(s) will actually be set again and again. Do Java and Android have some automatic mechanisms to avoid this kind of repetitive setting?

public class MyActivity extends Activity {
    ...
    MyAlarmReceiver alarm = new MyAlarmReceiver();
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // this set the alarm for message notification
        alarm.setAlarm(this);
    }
    ...
}

and,

public class MyAlarmReceiver extends WakefulBroadcastReceiver {
    ...
    public void onReceive(Context context, Intent intent) {
        Intent service = new Intent(context, MyService.class);
        // Start the service, keeping the device awake while it is launching.
        startWakefulService(context, service);
        // END_INCLUDE(alarm_onreceive)
    }

    public void setAlarm(Context context) {
        // This intent is used in alarm
        Intent intent = new Intent(context.getApplicationContext(), MyAlarmReceiver.class);
        // Create a PendingIntent to be triggered when the alarm goes off
        PendingIntent pIntent = PendingIntent.getBroadcast(context, reqCode, intent, PendingIntent.FLAG_ONE_SHOT);
        ... //Set the alarm
    }
}

and MyService is just a class extends Service.

asked 34 secs ago

Let's block ads! (Why?)



About AlarmManager in Android

Css columns height incorrect in Safari only

Vote count: 0

This code is working fine in Chrome/Firefox, but in Safari the content below my 3-column div is being pushed down much further than it should, as if the column content was condensed within a single column.

The site can be seen here: http://chrs.cblls.com

enter image description here

Relevant code:

.postIntro {
  width: 100%;
  display: inline-block;
  margin-top: 18px;
  column-count: 3;
  column-gap: 30px;
  column-fill: balance;
}

asked 15 secs ago

Let's block ads! (Why?)



Css columns height incorrect in Safari only