Is there a way to transform a nominal DataFrame into a Bubble Plot in Altair? - altair

I have the following code:
test_dict = {'A': ['a', 'b', 'c'], 'B': ['d', 'e', 'f'], 'C': ['g', 'h', 'i'],
'D': ['j', 'k', 'l'], 'E': ['m', 'n', 'o'], 'F': ['p', 'q', 'r'],
'G': ['s', 't', 'u'], 'H': ['v', 'w', 'x']}
test_df = pd.DataFrame(test_dict)
which produces the following DataFrame:
Is there a way to transform this into a BubblePlot in Altair? I want the columns (A - H) to make up the y-axis, and I want there to be a bubble for each entry in the column (so in this case, three entries per column). Can this be done?

You have wide-form data that you need to transform to long-form data (See See Wide-form vs. Long-form data in Altair's docs). You can generally address this with the Fold Transform:
import pandas as pd
import altair as alt
test_dict = {'A': ['a', 'b', 'c'], 'B': ['d', 'e', 'f'], 'C': ['g', 'h', 'i'],
'D': ['j', 'k', 'l'], 'E': ['m', 'n', 'o'], 'F': ['p', 'q', 'r'],
'G': ['s', 't', 'u'], 'H': ['v', 'w', 'x']}
test_df = pd.DataFrame(test_dict)
alt.Chart(test_df).transform_fold(
list(test_df.columns), as_=['key', 'value']
).mark_point().encode(
x='value:N',
y='key:N',
)

Related

How do I turn a string into its value pairs from a dictionary?

I have a dictionary with 13 key value pairs as follows:
dict = {'a': 'd', 'b': 'e', 'c': 'f', 'g': 'j', 'h': 'k', 'i': 'l', 'm': 'p', 'n': 'q', 'o': 'r', 's': 'v', 't': 'w', 'u': 'x', 'y': 'z'}
I want to create a function that takes a string as an argument and returns a string containing the corresponding values.
I have tried this:
def encoding(x):
for key in dict:
print (dict[key])
print (encoding('abc'))
However I am getting the wrong output. Firstly its printing each key on a new line. Secondly it keeps printing the values, not just the ones corresponding with 'abc'.
If the string passed to the function is 'abc' I want it to output 'def'.
Maybe this will help you
dict = {'a': 'd', 'b': 'e', 'c': 'f', 'g': 'j', 'h': 'k', 'i': 'l', 'm': 'p', 'n': 'q', 'o': 'r', 's': 'v', 't': 'w', 'u': 'x', 'y': 'z'}
def encoding(x):
return ''.join(dict[ch] for ch in x)
print (encoding('abc'))
There are many ways to do that. Here I have given one solution as similar to what you have done. One problem with your solution was you were passing the keys of the dictionary and not the input characters of the string.
Also, remember a Dictionary has no guaranteed order, so you use it only to quickly lookup a key and find a corresponding value, or you enumerate through all the key-value pairs without caring what the order is.
dict = {'a': 'd', 'b': 'e', 'c': 'f'}
def encoding(x):
for value in x:
print (dict[value])
print (encoding('abc'))
Hope this helps you.

How do you rearrange a list of lists where the first element holds multiple values and creates a new list of lists while repeating the first element?

Given a list of lists:
list_format = [['a', 'c', 'f', 'b'], ['j', 'l', 'o', 'c'], ['q', 's', 'v', 'e']]
'c', 'f', 'b' must be mapped to 'a'
'l', 'o', 'c' must be mapped to 'j'
's', 'v', 'e' must be mapped to 'q'
The output should look like this:
[['a','c'],['a','f'],['a','b'],['j','l'],['j','o'],['j','c'],['q','s'],['q','v'],['q','e']]
I've tried so far:
list_dict = {element[0]:element[1:] for element in list_format}
newer_lst = []
for key, value in list_dict.items():
newer_lst.append((key, value))
newer_lst
Gives me the output of tuples:
[('a', ['c', 'f', 'b']), ('j', ['l', 'o', 'c']), ('q', ['s', 'v', 'e'])]
I'm newer at this and trying to rearrange, any advice would be awesome, been stuck for days with trial and error(searched google countless times and constantly googling. I feel I'm getting close but can't seem to put it together.
Here is a one-liner, using slicing:
[[i[0],j] for i in list_format for j in i[1:]]
gives:
[['a', 'c'], ['a', 'f'], ['a', 'b'], ['j', 'l'], ['j', 'o'], ['j', 'c'], ['q', 's'], ['q', 'v'], ['q', 'e']]
Also, if you iterate through your value variable, you get your result:
list_dict = {element[0]:element[1:] for element in list_format}
newer_lst = []
for key, value in list_dict.items():
for i in value:
newer_lst.append((key, i))
print(newer_lst)
You don't need to create a loop, just loop on sub array and append new sub array to main output array on the fly, like new_list.append([lst[0], item])
new_list = []
for lst in list_format:
for item in lst[1:]:
new_list.append([lst[0], item])
print(new_list)
#output
#[['a', 'c'], ['a', 'f'], ['a', 'b'], ['j', 'l'], ['j', 'o'], ['j', 'c'], ['q', 's'], ['q', 'v'], ['q', 'e']]

Is there a function to create a list of unique elements in unique index using python?

Is there a way to create a new set of lists of elements, with each element of the original list in a unique index in the following lists?
orginal_list=['r', 'g', 'b', 'y']
output: ['y', 'g', 'r', 'b'],['g', 'y', 'b', 'r'],['r', 'b', 'y', 'g'],['b', 'r', 'g', 'y']
or
output: ['y', 'r', 'b', 'g'],['g', 'y', 'r', 'b'],['r', 'b', 'g', 'y'],['b', 'g', 'y', 'r']
or
...
I have tried to use iterators.permutations, but this does not fit with the unique index requirement.
Easiest would be to rotate the list. Here is a simple generator function producing all such rotations:
def rots(lst):
for i in range(len(lst)):
yield lst[i:] + lst[:i]
>>> list(rots(['r', 'g', 'b', 'y']))
[['r', 'g', 'b', 'y'],
['g', 'b', 'y', 'r'],
['b', 'y', 'r', 'g'],
['y', 'r', 'g', 'b']]
The rotating guarantees that each element actually occurs in each index exactly once.

Mapping between matrices

I have 2 matrices:
list_alpha = [['a'],
['b'],
['c'],
['d'],
['e']]
list_beta = [['1', 'a', 'e', 'b'],
['2', 'd', 'X', 'X'],
['3', 'a', 'X', 'X'],
['4', 'd', 'a', 'c'],
And my goal is if a letter from list_alpha is in a sublist of list_beta, then the first element of that line in list_beta (the #) is added to the correct line in list_alpha.
So my output would be:
final_list = [['a', '1', '3', '4'],
['b', '1'],
['c', '4'],
['d', '2', '4'],
['e', '1']]
But I'm pretty new to python and coding in general and I'm not sure how to do this. Is there a way to code this? Or do I have to change the way the data is stored in either list?
Edit:
Changing list_alpha to a dictionary helped!
Final code:
dict_alpha = {'a': [], 'b': [], 'c': [], 'd': [], 'e':[]}
list_beta = [['1', 'a', 'e', 'b'],
['2', 'd', 'X', 'X'],
['3', 'a', 'X', 'X'],
['4', 'd', 'a', 'c'],
['5', 'X', 'X', 'e'],
['6', 'c', 'X', 'X']]
for letter in dict_alpha:
for item in list_beta:
if letter in item:
dict_alpha.get(letter).append(item[0])
print(dict_alpha)
You can use dict_alpha as same as list_alpha , then fix your for loop.
For example:
dict_alpha = [['a'],
['b'],
['c'],
['d'],
['e']]
list_beta = [['1', 'a', 'e', 'b'],
['2', 'd', 'X', 'X'],
['3', 'a', 'X', 'X'],
['4', 'd', 'a', 'c'],
['5', 'X', 'X', 'e'],
['6', 'c', 'X', 'X']]
for al in dict_alpha:
for bt in list_beta:
for i in range(1, len(bt)):
if (bt[i] == al[0]):
al.append(bt[0])
print(dict_alpha)
Output:
[['a', '1', '3', '4'],
['b', '1'],
['c', '4', '6'],
['d', '2', '4'],
['e', '1', '5']]
Hope to helpful!

Shuffle a 2d list in python

I am new to Python. I have a Python 2D array [[a,b,c],[d,e,f],[g,h,j]] and would like to shuffle the 3 inner lists but not the contents. I can't seem to access numpy but would think this is quite simple. shuffle does not seem to work for a 2d array as it returns none. HELP Please!!
I would like something like [[d,e,f],[a,b,c],[g,h,j]] for example...
You can use the function shuffle from random:
import random
arr = [list('abc'), list('def'), list('ghj')]
>>> arr
[['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'j']]
random.shuffle(arr)
Example output:
>>> arr
[['g', 'h', 'j'], ['d', 'e', 'f'], ['a', 'b', 'c']]
Or, you could do the same with numpy.random.shuffle:
import numpy as np
arr = [list('abc'), list('def'), list('ghj')]
>>> arr
[['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'j']]
np.random.shuffle(arr)
>>> arr
[['g', 'h', 'j'], ['a', 'b', 'c'], ['d', 'e', 'f']]

Resources