Calling a list of DataFrame index with index key value - python-3.x

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

Related

Fetching value from dataframe with certain condition

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

Counting frequency of element in a list and joining them

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

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

Function doesn't change value, when applied to nested list

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

How to combine rows in pandas

I have a dataset like this
df = pd.DataFrame({'a' : ['a', 'b' , 'b', 'a'], 'b': ['a', 'b' , 'b', 'a'] })
And i want to combine first two rows and get dataset like this
df = pd.DataFrame({'a' : ['a b' , 'b', 'a'], 'b': ['a b' , 'b', 'a'] })
no rules but first two rows. I do not know how to combine row so i 'create' method to combine by transpose() as below
db = df.transpose()
db["new"] = db[0].map(str) +' '+ db[1]
db.drop([0, 1], axis=1, inplace=True) # remove these two columns
cols = db.columns.tolist() # re order
cols = cols[-1:] + cols[:-1]
db = db[cols]
df = db.transpose() # reverse operation
df.reset_index()
It works but i think there is an easier way
You can simply add the two rows
df.loc[0] = df.loc[0]+ df.loc[1]
df.drop(1, inplace = True)
You get
a b
0 ab ab
2 b b
3 a a
A bit more fancy looking :)
df.loc[0]= df[:2].apply(lambda x: ''.join(x))
df.drop(1, inplace = True)

Resources