How to separate a float number in awk into two values? [closed] - awk

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
There is an input file with values
2 0.2
3 0.3
4 0.4
5 1.0
6 1.1
7 1.2
8 1.3
9 2.0
10 2.1
11 2.2
12 3.0
13 3.1
14 4.0
0 0.0
1 0.1
Which are produces by the part of the code:
BEGIN{
n=4
b=n/10
t=0
for (k=0.0; k<=n; k++){
for (j=0.0; j<=b; j+=0.1){
arr[t]=k+j
t++
}
b=b-0.1
}
for(n in arr){
printf(" %d %.1f\n ",n, arr[n] )}
The question is, how to get an output by separating the floating number.
An expected output has to be:
2 0 2
3 0 3
and so on..

Have you tried:
$ echo "0.1" | awk '{split($0,a,"."); print a[2]; print a[1]}'
This should give the desired output.

Related

using awk insert new data and adding how far away [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
i have a data set like this (file.txt) tab separated
1 a
3 b
5 c
7 d
8 e
12 f
17 g
20 h
When i want to add a new data
6
I want to create new column which includes remaining from new entry.
Desired output is like that:
1 a 5
3 b 3
5 c 1
6 0
7 d 1
8 e 2
12 f 6
17 g 11
20 h 14
I have tried:
awk -v new="6" '
BEGIN {
FS=OFS="\t"
{gsub(new,",\n")}
{print $0"\n"$2,$3=|new-$1|}
' file
$ awk -v new=6 '
BEGIN {
FS=OFS="\t"
}
{
if((new<$1)&&f=="") { # when greater than new values seen
print new # for the first time, act
f=1 # flag to print only once
}
print $1,$2,((v=new-$1)<0?-v:v) # abs with ternary operator
}' file
Output:
1 a 5
3 b 3
5 c 1
6
7 d 1
8 e 2
12 f 6
17 g 11
20 h 14

Sort Dates in Chronological order in pandas [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Can anyone suggest me how to sort the dates in Pandas? I tried some methods but couldn't able to get desired result
Index Date Confirmed
0 01-01-2020 2
1 01-02-2020 3
2 01-03-2020 1834
3 02-01-2020 23
4 02-02-2020 3
5 02-03-2020 5
First convert column type to datetime using pd.to_datetime then sort using pd.DataFrame.sort_values and then reset index.
df.Date = pd.to_datetime(df.Date, dayfirst=True)
df = df.sort_values('Date').reset_index(drop=True)
df
Date Confirmed
0 2020-01-01 2
1 2020-01-02 23
2 2020-02-01 3
3 2020-02-02 3
4 2020-03-01 1834
5 2020-03-02 5

Use awk to replace value from column A with value from column B [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I'm very new to coding so I am here seeking some help. I have the table below, and I need to do as follow:
Every time that
[All_SNPs]<[Informative_SNPs]
I need to replace negative numbers or number=0 in [All_SNPs] with the values in [Informative_SNPs]. I have tried with awk but I can't get my head around this. Thank you if you can help.
Input
ID Informative_SNPs All_SNPs
1 13 0
2 29 -27
3 15 18
4 10 0
5 11 -850
6 25 37
Output
ID Informative_SNPs All_SNPs
1 13 13
2 29 29
3 15 18
4 10 10
5 11 11
6 25 37
awk 'NR>1 && ($3<=0 || $3<$2) {$3=$2}1' file
Output:
ID Informative_SNPs All_SNPs
1 13 13
2 29 29
3 15 18
4 10 10
5 11 11
6 25 37

how to calculate change of values over time in pandas? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have a below dataframe and want to count the number of time the value got changed over time.
input dataframe:
class date value
A 2019-01-02 80
A 2019-02-02 80
A 2019-03-02 90
A 2019-04-02 20
A 2019-05-02 80
A 2019-06-02 Null
A 2019-06-03 70
A 2019-06-04 70
A 2019-06-05 20
B ...
output dataframe as below:
class count_of_val
A 6
reason: (80,90,20,80, null,70, 20)
IIUC, use:
(df.groupby('class', sort=False)['value']
.apply(lambda x: (x != x.shift()).sum()-1)
.reset_index(name='count_of_val'))
[out]
class count_of_val
0 A 6
You can use the diff() function of pandas-DataFrame
df['count_of_val']=np.where((df['value'].diff()).fillna(method="bfill")!=0,1,0)
df['count_of_val'].loc[df['class']=='A'].sum()
Output is:
6
Or if you like DataFrames:
df['count_of_val']=np.where((df['value'].diff()).fillna(method="bfill")!=0,1,0)
desired_class = 'A'
df_count = pd.DataFrame(columns = ['class', 'count_of_val'],
data = [[desired_class, df['count_of_val'].loc[df['class']==desired_class].sum()]])
df_count
Output:
class count_of_val
0 A 6
Compute the rolling difference
diff_kernel = np.array([1,-1])
df['change'] = df.groupby('class', as_index=False)['value'].transform(lambda s: np.array(np.convolve(s, diff_kernel ,'same'), dtype=bool))
Then you can sum it as bool:
change_sum = df.groupby('class')['change'].sum()

awk script to add numbers in one column [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How can I add (numbers on the 2nd column) using for or while loops in an awk script?
While numbers at $1 are random and in increasing order:
1 through 2 in first iteration,
1.1 through 2.1 in 2nd iteration,
1.2 through 2.2 in 3rd iteration,
1.3 through 2.3 in 4th iteration
... up to the end
That means with 0.1 increment at each iteration.
Expected output:
39 for 1st iteration
47 for 2nd iteration...
Input data:
1.0 1
1.1 3
1.2 4
1.3 3
1.4 5
1.5 7
1.6 10
2.0 6
2.1 9
2.2 2
2.3 8
2.4 0
3.0 4
3.2 5
4.0 8
4.1 6
5.0 7
6.0 6
7.0 7
8.7 9
9.8 2
Here, I multiply each $1 by 10 to avoid problems with imprecise decimal numbers.
awk -v max=$(tail -1 data | awk '{print $1*10}') '
{n = $1 * 10}
NR==1 {min = n}
{
for (i=min; i<=(max-10); i++) {
if (i <= n && n <= (i+10)) {
sum[i, i+10] += $2
}
}
}
END {
for (key in sum) {
split(key, a, SUBSEP)
printf "[%.1f,%.1f] = %d\n", a[1]/10, a[2]/10, sum[key]
}
}
' data | sort -n
output
[1.0,2.0] = 39
[1.1,2.1] = 47
[1.2,2.2] = 46
[1.3,2.3] = 50
...
[8.6,9.6] = 9
[8.7,9.7] = 9
[8.8,9.8] = 2