iterating through list with 2 variables - list

I have this list:
list=[0, 0.3, .6, .9, 1.2 ,1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5]
how can you iterate through the list to create values for i and j such as these pairs:
0 / 0.3
0.3 / .6
.6 / .9
to the inclusive end of the list ?
I tried:
a = iter(list)
for i, j in zip(a, a):
print(str(i) + " / " + str(j))
but the boundaries are not repeated and it does not include the last value of the list

found it:
list=[0, 0.3, .6, .9, 1.2 ,1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5]
a = iter(list)
b = iter(list[1::])
for i, j in zip(a,b):
print(str(i) + " / " + str(j) )
0 / 0.3
0.3 / 0.6
0.6 / 0.9
0.9 / 1.2
1.2 / 1.5
1.5 / 2
2 / 2.5
2.5 / 3
3 / 3.5
3.5 / 4
4 / 4.5
4.5 / 5

Related

pandas.DataFrame: How to div row by row [python]

I want to div row[i] by row[i+1] in pandas.DataFrame
row[i] = row[i+1] / row[i]
for example:
1 2 3 4
4 2 6 2
8 5 3 1
the result is
0.25 1 0.5 2
0.5 0.4 2 2
You can divide by div shifted DataFrame, last remove NaN row by dropna:
print (df)
a b c d
0 1 2 3 4
1 4 2 6 2
2 8 5 3 1
print (df.div(df.shift(-1), axis=1))
a b c d
0 0.25 1.0 0.5 2.0
1 0.50 0.4 2.0 2.0
2 NaN NaN NaN NaN
df = df.div(df.shift(-1), axis=1).dropna(how='all')
print (df)
a b c d
0 0.25 1.0 0.5 2.0
1 0.50 0.4 2.0 2.0
Another solution for remove last row is select by iloc:
df = df.div(df.shift(-1), axis=1).iloc[:-1]
print (df)
a b c d
0 0.25 1.0 0.5 2.0
1 0.50 0.4 2.0 2.0

get round numbers between 0 to 1

I have UISlider that produce numbers between 0 to 1,
0.0590829
0.0643739
..
I want to get the rounded number between them, like:
0.1
0.2
0.3
...
1.0
found this (in c):
float x = arc4random() % 11 * 0.1;
but its not working on swift
var x = arc4random() % 11 * 0.1;
//error: binary operator '*' cannot be applied to operands of type 'UInt32' and 'Double'
Thanks
Multiply by 10 to get values between 0.0 and 10.0
round to remove the decimal
divide by 10
Example:
let values = [0, 0.0643739, 0.590829, 0.72273, 1]
for value in values {
print("\(value) -> \(round(value * 10) / 10)")
}
// 0.0 -> 0.0
// 0.0643739 -> 0.1
// 0.590829 -> 0.6
// 0.72273 -> 0.7
// 1.0 -> 1.0

Pandas data-frame ungrouping functionality

I have a dataframe with 3 columns:
df1 = pd.DataFrame([[2, 2, 5, 7], [2, 5, 7.5, 10], [2, 5, 1, 3]]).T
df1.columns = ['col1', 'col2', 'col3']
df1
col1 col2 col3
0 2 2.0 2
1 2 5.0 5
2 5 7.5 1
3 7 10.0 3
Now I want to ungroup the 3rd column and get a longer dataframe with a new column col4, as shown below in df2:
df2 = pd.DataFrame([[2, 2, 2, 2, 2, 2, 2, 5, 7, 7, 7], [2, 2, 5, 5, 5, 5, 5, 7.5, 10, 10, 10], [2, 2, 5, 5, 5, 5, 5, 1, 3, 3, 3], [1, 2, 1, 2, 3, 4, 5, 1, 1, 2, 3]]).T
df2.columns = ['col1', 'col2', 'col3', 'col4']
df2
col1 col2 col3 col4
0 2 2.0 2 1
1 2 2.0 2 2
2 2 5.0 5 1
3 2 5.0 5 2
4 2 5.0 5 3
5 2 5.0 5 4
6 2 5.0 5 5
7 5 7.5 1 1
8 7 10.0 3 1
9 7 10.0 3 2
10 7 10.0 3 3
Here is one way to use groupby with reindex.
# custom apply function
def func(group):
return group.reset_index(drop=True).reindex(np.arange(group.col3)).fillna(method='ffill')
# groupby apply
result = df1.groupby(level=0).apply(func)
col1 col2 col3
0 0 2 2.0 2
1 2 2.0 2
1 0 2 5.0 5
1 2 5.0 5
2 2 5.0 5
3 2 5.0 5
4 2 5.0 5
2 0 5 7.5 1
3 0 7 10.0 3
1 7 10.0 3
2 7 10.0 3
result['col4'] = result.index.get_level_values(1) + 1
result.reset_index(drop=True)
col1 col2 col3 col4
0 2 2.0 2 1
1 2 2.0 2 2
2 2 5.0 5 1
3 2 5.0 5 2
4 2 5.0 5 3
5 2 5.0 5 4
6 2 5.0 5 5
7 5 7.5 1 1
8 7 10.0 3 1
9 7 10.0 3 2
10 7 10.0 3 3
You can also use numpy for faster calculation:
import numpy as np
import pandas as pd
df = pd.DataFrame([[2, 2, 5, 7], [2, 5, 7.5, 10], [2, 5, 1, 3]]).T
df.columns = ['col1', 'col2', 'col3']
x = df.values
n = df.iloc[:,-1].astype(int).values
data = np.repeat(x,n,axis=0)
df1 = pd.DataFrame(data)
df1.loc[:,3] = n.repeat(n)
df1.columns = ['col1','col2','col3','col4']
print(df1)
Gives:
col1 col2 col3 col4
0 2.0 2.0 2.0 2
1 2.0 2.0 2.0 2
2 2.0 5.0 5.0 5
3 2.0 5.0 5.0 5
4 2.0 5.0 5.0 5
5 2.0 5.0 5.0 5
6 2.0 5.0 5.0 5
7 5.0 7.5 1.0 1
8 7.0 10.0 3.0 3
9 7.0 10.0 3.0 3
10 7.0 10.0 3.0 3

Selecting data from an HDFStore by floating-point data_column

I have a table in an HDFStore with a column of floats f stored as a data_column. I would like to select a subset of rows where, e.g., f==0.6.
I'm running in to trouble that I'm assuming is related to a floating-point precision mismatch somewhere. Here is an example:
In [1]: f = np.arange(0, 1, 0.1)
In [2]: s = f.astype('S')
In [3]: df = pd.DataFrame({'f': f, 's': s})
In [4]: df
Out[4]:
f s
0 0.0 0.0
1 0.1 0.1
2 0.2 0.2
3 0.3 0.3
4 0.4 0.4
5 0.5 0.5
6 0.6 0.6
7 0.7 0.7
8 0.8 0.8
9 0.9 0.9
[10 rows x 2 columns]
In [5]: with pd.get_store('test.h5', mode='w') as store:
...: store.append('df', df, data_columns=True)
...:
In [6]: with pd.get_store('test.h5', mode='r') as store:
...: selection = store.select('df', 'f=f')
...:
In [7]: selection
Out[7]:
f s
0 0.0 0.0
1 0.1 0.1
2 0.2 0.2
4 0.4 0.4
5 0.5 0.5
8 0.8 0.8
9 0.9 0.9
[7 rows x 2 columns]
I would like the query to return all of the rows but instead several are missing. A query with where='f=0.3' returns an empty table:
In [8]: with pd.get_store('test.h5', mode='r') as store:
selection = store.select('df', 'f=0.3')
...:
In [9]: selection
Out[9]:
Empty DataFrame
Columns: [f, s]
Index: []
[0 rows x 2 columns]
I'm wondering whether this is the intended behavior, and if so is there is a simple workaround, such as setting a precision limit for floating-point queries in pandas? I'm using version 0.13.1:
In [10]: pd.__version__
Out[10]: '0.13.1-55-g7d3e41c'
I don't think so, no. Pandas is built around numpy, and I have never seen any tools for approximate float equality except testing utilities like assert_allclose, and that won't help here.
The best you can do is something like:
In [17]: with pd.get_store('test.h5', mode='r') as store:
selection = store.select('df', '(f > 0.2) & (f < 0.4)')
....:
In [18]: selection
Out[18]:
f s
3 0.3 0.3
If this is a common idiom for you, make a function for it. You can even get fancy by incorporating numpy float precision.

Need to create data frame from patterns in a string

I have the following string I need to extract the patterns into a single column data frame named SIZE
str <- "N · 0.1 [mm]: N · 0.1 + 0.02 [mm]: N · 0.1 + 0.05 [mm] N · 0.1 + 0.08 [mm] M · 1 [mm]: M · 1 + 0.5 [mm] M · 1 + 0.75 [mm]"
The patterns are either followed by : or whitespace and always ends in [mm]
The regex I am using to match my patterns is and it works, but i'm not sure how to extract the matches to create a column as a data frame.
\S\W+\d\.?\d?\s\+?\s?\d?\.?\d?\d?\s?\[mm\]
Output expected: 1 column named SIZE
N · 0.1 [mm]
N · 0.1 + 0.02 [mm]
N · 0.1 + 0.05 [mm]
N · 0.1 + 0.08 [mm]
M · 1 [mm]
M · 1 + 0.5 [mm]
M · 1 + 0.75 [mm]
Any help appreciated. Thanks..
Perhaps, strsplit would make things easier here..
str <- "N · 0.1 [mm]: N · 0.1 + 0.02 [mm]: N · 0.1 + 0.05 [mm] N · 0.1 + 0.08 [mm] M · 1 [mm]: M · 1 + 0.5 [mm] M · 1 + 0.75 [mm]"
vals <- strsplit(str, '(?<=\\])[\\s:]*', perl = T)
data.frame(SIZE = unlist(vals))
Output
SIZE
1 N · 0.1 [mm]
2 N · 0.1 + 0.02 [mm]
3 N · 0.1 + 0.05 [mm]
4 N · 0.1 + 0.08 [mm]
5 M · 1 [mm]
6 M · 1 + 0.5 [mm]
7 M · 1 + 0.75 [mm]
Here's one approach to get the data in: replace any instances of "[mm] " with "[mm]: " and scan the text in with ":" as your separator. No fussing with regexes....
scan(what = "", text = gsub("[mm] ", "[mm]: ", str, fixed=TRUE),
sep = ":", strip.white=TRUE)
# Read 7 items
# [1] "N · 0.1 [mm]" "N · 0.1 + 0.02 [mm]" "N · 0.1 + 0.05 [mm]"
# [4] "N · 0.1 + 0.08 [mm]" "M · 1 [mm]" "M · 1 + 0.5 [mm]"
# [7] "M · 1 + 0.75 [mm]"
Just assign the result there to a column in a data.frame or create a data.frame with the output. Or, all in one:
data.frame(
SIZE = scan(text = gsub("[mm] ", "[mm]: ", str, fixed=TRUE),
sep = ":", strip.white=TRUE, what = ""))