I have a function which iterates over list of lists. If it finds the value, which is a list itself, it should create a string from this value and insert it instead of the original one:
def lst_to_str(lst):
for x in lst:
for y in x:
i = 0
if type(y) == list:
x[i] = ",".join(y)
i +=1
return lst
The problem is, when I apply this function to pd.DataFrame column
df['pdns'] = df['pdns'].apply(lambda x: lst_to_str(x))
It returns me the original nested list:
[['a', 'b', 'c', 'd'], ['a1', 'b1', 'd1', 'c1'],['a2', 'b2', 'c2', ['d2_1', 'd2_2']]]
Instead of:
[['a', 'b', 'c', 'd'], ['a1', 'b1', 'd1', 'c1'],['a2', 'b2', 'c2', 'd2_1, d2_2']]
Your code is wrong. In your function definition, you're not making any change to 1st and in the end you return 1st. You're checking some condition and then you change the value of your counter (x). Correct this problem and try again
Related
I have a dataframe which is containing 3 columns (['A','B','C]) and 3 rows in it.
We are using a for loop to fetch value(storing into variable) from above dataframe based upon certain condition from column B.
Further we are using list to store value present in variable.
Here question is upon checking list value, we are getting variable value, its type.
I'm not sure why it is happening. As list should contain only variable value only.
Please can anyone help us to get ideal solution for same.
Thanks,
Bhuwan
dataframe: columns-A,B,C rows value- a to i :df = ([a,b,c][d,b,f][g,b,i]).
list_1=[]
for i in range(0,9):
variable_1=df['A'][df.B == 'b']
list_1.append(variable_1)
print(list_1):
Ideal output: ['a','d','g']
while we are getting output as
['a type: object','d type: object','g type: object'].
You can get your ideal output like this:
import pandas as pd
df = pd.DataFrame({'A': ['a', 'd', 'g'], 'B': ['b', 'b', 'b'], 'C': ['c', 'f', 'i']})
list_1 = list(df[df['B'] == 'b']['A'].values) # <- this line
print(list_1)
> ['a', 'd', 'g']
You just need:
1) to filter your dataframe by column "B" df[df['B'] == 'b']
2) and only then take values of the resulted column "A", turning them into list
I have a list of list. I want to obtain the frequency of element in the inner list and concatenate that with the element in the outer list.
aa =['a', ['b', 'b', 'b', 'b', 'd', 'd']]
I try to use Counter to get the frequency of occurrence of each element in the inner list as :
from collections import Counter
Counter(aa[1])
It gives:
Counter({'b': 4, 'd': 2})
I want to concatenate this with the outer list element and obtain as follows:
'ab4d2'
I can also iterate through the Counter and get key, value in a list:
y = []
for k, v in surr.items():
y.append(str(k) + str(v))
Output: ['O4', 'Sb2']
There are many answers to get the frequency of occurrence but I did not find any which does this (the problem is joining with outer 'a' in an efficient way) . Could anyone please help me on this. Thanks in advance.
You can use a generator expression with str.join:
aa[0] + ''.join('%s%d' % t for t in Counter(aa[1]).items())
Given aa = ['a', ['b', 'b', 'b', 'b', 'd', 'd']], this returns:
ab4d2
I need to convert a single input string into a dictionary with its place indices as keys and the letters as values in python. But I'm stuck here. Any help will be appreciated.
r = list("abcdef")
print (r)
for index,char in enumerate(r,0):
indd = str(index)
print(indd)
abc = indd.split(",")
list2 = list(abc)
d = dict(zip(list2,r))
print(d)
This is one approach using range.
Demo:
r = list("abcdef")
d = {}
for i in range(len(r)):
d[i] = r[i]
print(d)
Output:
{0: 'a', 1: 'b', 2: 'c', 3: 'd', 4: 'e', 5: 'f'}
Or a simpler approach using dict().
r = list("abcdef")
d = dict(zip(range(len(r)), r))
print(d)
You have a string abcdef.
First make a list of tuples having first element of tuple as place index and second element of the tuple as the letter at that index. This can be done in this way:
tuples = list(enumerate("abcdef"))
Now, using the dictionary constructor, convert this list of tuples to a dictionary as given below:
dict(tuples)
Demo:
>>> dict(list(enumerate("abcdef")))
{0: 'a', 1: 'b', 2: 'c', 3: 'd', 4: 'e', 5: 'f'}
df = pd.DataFrame([[3,3,3]]*4,index=['a','b','c','d'])
While we can extract a copy of a section of an index via specifying row numbers like below:
i1=df.index[1:3].copy()
Unfortunately we can't extract a copy of a section of an index via specifying the key (like the case of df.loc method). When I try the below:
i2=df.index['a':'c'].copy()
I get the below error:
TypeError: slice indices must be integers or None or have an __index__ method
Is there any alternative to call a subset of an index based on its keys? Thank you
Simpliest is loc with index:
i1 = df.loc['b':'c'].index
print (i1)
Index(['b', 'c'], dtype='object')
Or is possible use get_loc for positions:
i1 = df.index
i1 = i1[i1.get_loc('b') : i1.get_loc('d') + 1]
print (i1)
Index(['b', 'c'], dtype='object')
i1 = i1[i1.get_loc('b') : i1.get_loc('d') + 1]
print (i1)
Index(['b', 'c', 'd'], dtype='object')
Alternative:
i1 = i1[i1.searchsorted('b') : i1.searchsorted('d') + 1]
print (i1)
Index(['b', 'c', 'd'], dtype='object')
Try using .loc, see this documentation:
i2 = df.loc['a':'c'].index
print(i2)
Output:
Index(['a', 'b', 'c'], dtype='object')
or
df.loc['a':'c'].index.tolist()
Output:
['a', 'b', 'c']
Is there method or some smart way that's easy to read to make a combination of elements in Groovy? I'm aware of Iterable#combinations or GroovyCollections#combinations but it makes Partial permutation with repetition as I understand it so far. See example.
// Groovy combinations result
def e = ['a', 'b', 'c']
def result = [e, e].combinations()
assert [['a', 'a'], ['b', 'a'], ['c', 'a'], ['a', 'b'], ['b', 'b'], ['c', 'b'], ['a','c'], ['b', 'c'], ['c', 'c']] == result
// What I'm looking for
def e = ['a', 'b', 'c']
def result = ???
assert [['a', 'b'], ['a', 'c'], ['b', 'c']] == result
Feel free to post alternate solutions. I'm still looking for better readability (it's used in script for non-developers) and performance (w/o unnecessary iterations).
I'm not so sure about the readability, but this should do the trick.
def e = ['a', 'b', 'c']
def result = [e, e].combinations().findAll { a, b ->
a < b
}
assert [['a', 'b'], ['a', 'c'], ['b', 'c']] == result
Note that if a element occur twice in the list its combinations will also occur twice. Add a '.unique()' at the end if they are unwanted
Here's a more generalized approach that allows you to specify the "r" value for your nCr combinations. It does this by storing permutations in Sets, with the Sets providing the uniqueness:
// returns combinations of the input list of the provided size, r
List combinationsOf(List list, int r) {
assert (0..<list.size()).contains(r) // validate input
def combs = [] as Set
list.eachPermutation {
combs << it.subList(0, r).sort { a, b -> a <=> b }
}
combs as List
}
// the test scenario...
def e = ['a', 'b', 'c']
def result = combinationsOf(e, 2)
assert [['a', 'b'], ['a', 'c'], ['b', 'c']] == result