If need test substrings use Series.str.contains:
conditions = [
(df['column'].str.contains('one')),
(df['column'].str.contains('two')),
(df['column'].str.contains('three')),
(df['column'].str.contains('other'))]
If need exact match use Series.eq or ==:
conditions = [
(df['column'].eq('one')),
(df['column'].eq('two')),
(df['column'].eq('three')),
(df['column'].eq('other'))]
conditions = [
(df['column'] == 'one'),
(df['column'] == 'two'),
(df['column'] == 'three'),
(df['column'] == 'other')]
print (new_df)
column Int
0 one 1
1 ones 1
2 other 0
3 two 2
4 twos 2
5 others 0
6 three 3
7 threes 3
Answer from jezrael on Stack OverflowWe can use np.core.defchararray.find to find the position of foo string in each element of bar, which would return -1 if not found. Thus, it could be used to detect whether foo is present in each element or not by checking for -1 on the output from find. Finally, we would use np.flatnonzero to get the indices of matches. So, we would have an implementation, like so -
np.flatnonzero(np.core.defchararray.find(bar,foo)!=-1)
Sample run -
In [91]: bar
Out[91]:
array(['aaa', 'aab', 'aca'],
dtype='|S3')
In [92]: foo
Out[92]: 'aa'
In [93]: np.flatnonzero(np.core.defchararray.find(bar,foo)!=-1)
Out[93]: array([0, 1])
In [94]: bar[2] = 'jaa'
In [95]: np.flatnonzero(np.core.defchararray.find(bar,foo)!=-1)
Out[95]: array([0, 1, 2])
Look at some examples of using in:
In [19]: bar = np.array(["aaa", "aab", "aca"])
In [20]: 'aa' in bar
Out[20]: False
In [21]: 'aaa' in bar
Out[21]: True
In [22]: 'aab' in bar
Out[22]: True
In [23]: 'aab' in list(bar)
It looks like in when used with an array works as though the array was a list. ndarray does have a __contains__ method, so in works, but it is probably simple.
But in any case, note that in alist does not check for substrings. The strings __contains__ does the substring test, but I don't know any builtin class that propagates the test down to the component strings.
As Divakar shows there is a collection of numpy functions that applies string methods to individual elements of an array.
In [42]: np.char.find(bar, 'aa')
Out[42]: array([ 0, 0, -1])
Docstring:
This module contains a set of functions for vectorized string operations and methods. The preferred alias fordefchararrayisnumpy.char.
For operations like this I think the np.char speeds are about same as with:
In [49]: np.frompyfunc(lambda x: x.find('aa'), 1, 1)(bar)
Out[49]: array([0, 0, -1], dtype=object)
In [50]: np.frompyfunc(lambda x: 'aa' in x, 1, 1)(bar)
Out[50]: array([True, True, False], dtype=object)
Further tests suggest that the ndarray __contains__ operates on the flat version of the array - that is, shape doesn't affect its behavior.
Create a mapping of each country -> area, then use explode and map to apply the mapping and finally, use groupby and apply to rebuild the list:
geo = {'Europe': Europe, 'North_America': North_America, 'Asia': Asia}
mapping = {country: area for area, countries in geo.items() for country in countries}
df['geo'] = df['location'].str.split(', ').explode().map(mapping) \
.groupby(level=0).apply(', '.join)
Output:
>>> df
location geo
0 London, Paris Europe, Europe
1 Hong Kong Asia
2 London, New York Europe, North_America
3 Singapore, Toroto Asia, North_America
4 Boston North_America
By using NumPy library together with python for loops we can get the results. At first we combine lists of country's cities together and then create another list named continents which length is the same as the created list of cities:
import numpy as np
import pandas as pd
continents = ["Europe"] * len(Europe) + ["North_America"] * len(North_America) + ["Asia"] * len(Asia)
countries = Europe + North_America + Asia
locations = data['location']
Then for each city, even for each in the combinations, we find its index in the created country list. Then we create a list for number of commas in each of that combinations for using to create the desired output with commas:
corsp = []
comma_nums = []
for i in locations:
for j, k in enumerate(i.split(', ')):
corsp.append(np.where(np.array(countries) == k)[0][0])
comma_nums.append(j)
continents list will be reordered and modified by created index list. Then its arguments combined in list format as the combination style which where in locations, and then the lists convert to strings as they are needed for the output:
reordered_continents = [continents[i] for i in corsp]
mod_continents = []
iter = 0
f = 1
for i in comma_nums:
mod_continents.append(reordered_continents[iter:i + f])
iter = i + f
f = iter + 1
for i, j in enumerate(mod_continents):
if len(j) > 1:
for k in j:
mod_continents[i] = ', '.join(j)
else:
mod_continents[i] = ''.join(j)
df['geo'] = mod_continents
Why not simple comprehension lists:
[u[:2] for u in HHMM]
#Out[39]: ['00', '01', '02', '03', '04', '05', '06', '07', '08', '09']
[u[-2:] for u in HHMM]
#Out[40]: ['00', '01', '02', '03', '04', '05', '06', '07', '08', '09']
Example:
for i in range(len(HHMM)):
hour = HHMM[i][0:2]
minute = HHMM[i][2:]
print('Time: {}:{}'.format(hour, minute))
Output:
Time: 00:00
Time: 00:01
Time: 00:02
Time: 00:03
Time: 00:04
Time: 00:05
Time: 00:06
Time: 00:07
Time: 00:08
Time: 00:09
EDIT:
HHMM = ['0000', '0001', '0002', '0003', '0004', '0005', '0006', '0007', '0008', '0009']
hours = []
minutes = []
for i in range(len(HHMM)):
hours.append(HHMM[i][0:2])
minutes.append(HHMM[i][2:])
print('Hours: {}'.format(list(hours)))
print('Minutes: {}'.format(list(minutes)))
print(a[arr_index])
not array_index!!
a = np.array(['apple', 'orange', 'apple', 'banana'])
arr_index = np.where(a == 'apple')
print(arr_index)
print(a[arr_index])
I believe an easier way is to just do :
A = np.array(['apple', 'orange', 'apple', 'banana'])
arr_index = np.where(A == 'apple')
print(arr_index)
And you get:
(array([0, 2]),)