A good way to store key to multiple values in Pharo Smalltalk? - smalltalk

I have a key value storage (dictionary) made up of:
key1 value1
key2 value1
key3 value3
key1 value4
key3 value4
and so on
I'd like to group these so the keys only appear once (sorting of keys is optional) and values:
key1 value1, value4
key2 value1
key3 value3, value4
The question: Is there a dedicated way to store the result?

Managed to fix it. Used a key value storage (dictionary) where the keys are key1, key2, key3 and the values are a Set (or Array) of value1, value2, value3 etc.
Something along the line of:
keys do: [ :eachKey | (eachKey condition) ifTrue: [
keyValueStorage at: eachKey ifAbsentPut: [ Set new. ]
( keyValueStorage at: eachKey ) add: value. ] ] .

Related

Search Keywords in DataFrame by Column Value

I need to know if every value of one column in a dataframe have some keywords from another column. For example:
ITEM Column1
Item1 A
Item1 B
Item1 C
Item2 A
Item2 B
Item2 C
Item3 A
Item3 B
Item3 D
I want to know for every item in the column item, if I have the values A and C from column 1. In that case, my output should be Item 1 and Item 2.
Thanks!
The following code will create a df with the Items that the value of Column1 of them is A or B:
filtered_df = df[df['Column1'] == A or df['Column1'] == B]
Now, you can select filtered_df['ITEM'] in order to take the corresponded items. In case you just want the set of items then select filtered_df['ITEM'].drop_duplicates().

pandas - transform dataframe column values in to rows

I have a simple question that has probably been answered before however I searched and couldn't find a working solution. So, I got a dataframe that looks like this -
0 Key Key1 Key2 Key3
1 value1 value2 value3 value4
How could I transform it in to something like this ?
0 Keys_Colum Values_Column
1 Key value
2 Key1 value1
3 Key2 value2
4 Key3 value3
basically transforming a dataframe of two rows with multiple columns in to a dataframe of two columns and multiple rows.
A simple transpose work just fine:
df.T.rename(columns={
0: 'Keys_Column',
1: 'Values_Column'
})

Turn a dictionary with one key and multiple values into a dataframe

I have a dictionary comprised of one key and multiple values.
dict = {'key1':['value1','value2']}
I would to take this dictionary and turn it into a pandas dataframe in the following form:
column1 column2
key1 value1
key1 value2
Where for every value, key is indicated.
Thank you!
Try:
dct = {'key1':['value1','value2']}
pd.DataFrame(dct).melt()
Output:
variable value
0 key1 value1
1 key1 value2
How about pd.DataFrame(my_dict).stack()?

Pandas MultiIndex with some varying numbers of levels

Is it possible for a pandas multiIndex to have varying numbers of levels? For example, if you were working with indices A1, A2, and B, so that A has sublevels 1 and 2, but B has no sublevels? If so, how do you do it?
For example, could you produce a dataframe that looked like this:
A B
1 2
val1 val2 val3
The reason that I'm hoping to avoid adding a sublevel to B is that I'm going to use this dataframe to create a JSON object that looks like this:
[
{
A: {
1: val1,
2: val2
},
B: val3
},
...
]
MultiIndex doesn't support this kind of behavior, but what you could do is rearrange the DataFrame to look like this:
>>> d = {'A': {1: 'val1', 2: 'val2'}, 'B': 'val3'}
>>> pd.DataFrame(d)
A B
1 val1 val3
2 val2 val3
This way A[1] still points to val1 and A[2] still points to val2, but the only complication would be that you have to treat B as a Series.

Comma separated values to individual columns in Kusto

I have two columns in kusto table, The second column has comma separated values, and I need the values to be projected as individual columns. The comma sepearted values in second column, changes for each environment, and it cannot be hardcoded.
Input:
key val
key1 val1,val2,val3,val4
key2 val8,val2,val9,val4
key3 val8,val1,val9,val5
output:
keyhdr valhdr1 valhdr2 valhdr3 valhdr4
key1 val1 val2 val3 val4
key2 val8 val2 val9 val4
key3 val8 val1 val9 val5
You can first use the split function to create an array from the tabular expression, then you can use array indexing to extend it into columns. Note that if no value is found in the index it will be filled with an empty string.
This will give you the output you wanted :
datatable(key:string, val:string)
[
"key1","val1,val2,val3,val4",
"key2", "val8,val2,val9,val4",
"key3", "val8,val1,val9,val5"
]
| extend all=split(val,',')
| extend valhdr1 = all[0]
| extend valhdr2 = all[1]
| extend valhdr3 = all[2]
| extend valhdr4 = all[3]
| project key,valhdr1,valhdr2,valhdr3,valhdr4