I have the following script and I have problems on >printing and
saving. Any ideas or help welcome.
import pandas as pd
import numpy as np
df = pd.DataFrame({'A': [3513, 3514, 3517],
'B':['lname1', 'lname2', 'lname3'],
'C':['fname1', 'fname2', 'fname3'],
},index=np.arange(3,dtype=int))
def vamos(df):
for x in df.index:
s = (df.loc[x,'A'])
digits = list(map(int, str(s)))
Sum = (sum(digits))
df = df.assign(column_2=(Sum))
df['column_3'] = 20 - Sum
print(df)
df.to_excel("book_Sum.xlsx")
if __name__ == '__main__':
vamos(df)
This is what I get with print(df):
A B C column_2 column_3
0 3513 lname1 fname1 12 8
1 3514 lname2 fname2 12 8
2 3517 lname3 fname3 12 8
A B C column_2 column_3
0 3513 lname1 fname1 13 7
1 3514 lname2 fname2 13 7
2 3517 lname3 fname3 13 7
A B C column_2 column_3
0 3513 lname1 fname1 16 4
1 3514 lname2 fname2 16 4
2 3517 lname3 fname3 16 4
And this when I save to excel. df.to_excel("book_Sum.xlsx")
A B C column_2 column_3
0 3513 lname1 fname1 16 4
1 3514 lname2 fname2 16 4
2 3517 lname3 fname3 16 4
Related
I think this is be an IB API more than the IBrokers R package.
I am using reqHistoricalData to get 30 minutes intraday historical data. The market is open and I am not getting the same day's data. I only get yesterday's data.
Is it possible to get the same day intraday bar data?
here is the code I am using, it only gives data for the previous day, not same day.
library(tidyverse)
library(IBrokers)
tws = twsConnect()
contract <- twsEquity('VOD','SMART')
VOD_intraday = IBrokers::reqHistoricalData(tws, Contract = contract, endDateTime = "20210408 13:24:28", barSize = "1 min", duration = "1 D")
VOD_intraday %>% as.data.frame() %>% rownames_to_column(var = "time") %>% arrange(desc(time)) %>% head()
It's 13:27 GMT on 2021-04-08 and London is open. And here is the response - it only gives data from 2020-04-07:
> contract <- twsEquity('VOD','SMART')
> VOD_intraday = IBrokers::reqHistoricalData(tws, Contract = contract, endDateTime = "20210408 13:24:28", barSize = "1 min", duration = "1 D")
waiting for TWS reply on VOD .... done.
> VOD_intraday %>% as.data.frame() %>% rownames_to_column(var = "time") %>% arrange(desc(time)) %>% head()
time VOD.Open VOD.High VOD.Low VOD.Close VOD.Volume VOD.WAP VOD.hasGaps VOD.Count
1 2021-04-07 20:59:00 18.96 18.98 18.95 18.98 1131 18.958 0 265
2 2021-04-07 20:58:00 18.96 18.96 18.95 18.96 90 18.957 0 42
3 2021-04-07 20:57:00 18.96 18.97 18.95 18.95 258 18.960 0 72
4 2021-04-07 20:56:00 18.96 18.96 18.95 18.95 124 18.959 0 58
5 2021-04-07 20:55:00 18.96 18.96 18.95 18.96 56 18.958 0 34
6 2021-04-07 20:54:00 18.95 18.96 18.95 18.95 26 18.951 0 12
Instead of VOD, you can use SPY, MSFT or any US security while the US market is open.
Edit: It turns out you need realtime subscription to get same day data. The answer below works.
One has to specify the ending time, or leave it blank to get the most recent data available.
Try this:
VOD_intraday = IBrokers::reqHistoricalData(tws, Contract = contract, endTime = "", barSize = "1 min", duration = "1 D")
Here's the execution when I run it:
> library(tidyverse)
> library(IBrokers)
IBrokers version 0.9-10. Implementing API Version 9.64
IBrokers comes with NO WARRANTY. Not intended for production use!
See ?IBrokers for details.
> tws = twsConnect()
> contract <- twsEquity('SPY','SMART')
> VOD_intraday = IBrokers::reqHistoricalData(tws, Contract = contract, endDateTime = "", barSize = "1 min", duration = "1 D")
waiting for TWS reply on SPY ........... done.
> head(VOD_intraday)
SPY.Open SPY.High SPY.Low SPY.Close SPY.Volume SPY.WAP SPY.hasGaps SPY.Count
2021-04-08 07:30:00 407.93 407.98 407.68 407.80 5042 407.846 0 1709
2021-04-08 07:31:00 407.81 408.00 407.74 407.98 1615 407.844 0 1065
2021-04-08 07:32:00 407.99 408.05 407.81 407.90 2451 407.932 0 1560
2021-04-08 07:33:00 407.89 407.98 407.88 407.95 2353 407.932 0 1300
2021-04-08 07:34:00 407.95 407.97 407.81 407.81 1708 407.907 0 1012
2021-04-08 07:35:00 407.82 407.86 407.61 407.67 2729 407.726 0 1458
And for symbol VOD:
> contract <- twsEquity('VOD','SMART')
> VOD_intraday = IBrokers::reqHistoricalData(tws, Contract = contract, endDateTime = "", barSize = "1 min", duration = "1 D")
waiting for TWS reply on VOD .... done.
> head(VOD_intraday)
VOD.Open VOD.High VOD.Low VOD.Close VOD.Volume VOD.WAP VOD.hasGaps VOD.Count
2021-04-08 07:30:00 18.95 18.95 18.91 18.92 246 18.921 0 49
2021-04-08 07:31:00 18.91 18.91 18.90 18.90 69 18.905 0 31
2021-04-08 07:32:00 18.90 18.90 18.87 18.87 237 18.881 0 44
2021-04-08 07:33:00 18.87 18.87 18.86 18.87 45 18.870 0 20
2021-04-08 07:34:00 18.87 18.87 18.85 18.86 173 18.860 0 57
2021-04-08 07:35:00 18.86 18.87 18.85 18.86 39 18.859 0 19
I want to join two datasets.
Datasets had same columns/ variables.
Dataset 1 (n11)
caseid v000 v005 age v021 v022 v023 v024 resi region v102 education pregnant v445 v501 v717 wealth stra occupation
1 101 15 2 NP6 342191 5 101 10 1 1 2 1 2 1 0 2190 1 4 4 NPIR61FL$ssubreg=1, NPIR61FL$v025=2 2
2 101 19 2 NP6 342191 7 101 10 1 1 2 1 2 0 0 2300 1 4 3 NPIR61FL$ssubreg=1, NPIR61FL$v025=2 2
3 101 19 4 NP6 342191 2 101 10 1 1 2 1 2 1 0 2139 1 4 3 NPIR61FL$ssubreg=1, NPIR61FL$v025=2 2
4 101 21 4 NP6 342191 5 101 10 1 1 2 1 2 0 0 1855 1 4 2 NPIR61FL$ssubreg=1, NPIR61FL$v025=2 2
5 101 45 3 NP6 342191 5 101 10 1 1 2 1 2 0 0 2133 3 4 3 NPIR61FL$ssubreg=1, NPIR61FL$v025=2 2
6 101 47 2 NP6 342191 1 101 10 1 1 2 1 2 2 0 2022 1 4 4 NPIR61FL$ssubreg=1, NPIR61FL$v025=2 2
Dataset 2 (n16)
caseid v000 v005 age v021 v022 v023 v024 resi region v102 education pregnant v445 v501 v717 wealth stra occupation
1 101 2 2 NP5 295061 6 101 1 1 1 2 1 2 0 0 2534 1 4 2 NPIR51FL$v024=1, NPIR51FL$v025=2 2
2 101 2 3 NP5 295061 1 101 1 1 1 2 1 2 2 0 2061 0 4 2 NPIR51FL$v024=1, NPIR51FL$v025=2 2
3 101 3 3 NP5 295061 1 101 1 1 1 2 1 2 2 0 2157 1 4 1 NPIR51FL$v024=1, NPIR51FL$v025=2 2
4 101 4 1 NP5 295061 4 101 1 1 1 2 1 2 0 0 2370 1 4 1 NPIR51FL$v024=1, NPIR51FL$v025=2 2
5 101 6 6 NP5 295061 2 101 1 1 1 2 1 2 3 0 2254 0 4 2 NPIR51FL$v024=1, NPIR51FL$v025=2 2
6 101 7 2 NP5 295061 2 101 1 1 1 2 1 2 2 0 2364 0 4 1 NPIR51FL$v024=1, NPIR51FL$v025=2 2
I used rbind function.
code was d2 <-rbind(n11, n16)
I found the following error. How can I fix this?
Error: Can't convert from <labelled<double>> to <labelled<double>> due to loss of precision.
* Locations: 5723, 5724, 5725, 5726, 5727, 5728, 5729, 5730, 5731, 5732, 5733, 5734, 5735, 5736, 5...
Values are labelled in `` but not in ``.
Run `rlang::last_error()` to see where the error occurred.
I have found the solution now.
We can use full_join function of dplyr package to solve this.
d2<- full_join(n11, n16)
Define a function to implement Fibonacci Sequence: 1, 1, 2, 3, 5, 8, 13, 21, 34. Please use the function output first 20 figures of Fibonacci Sequence.
Here is a python implementation
def fib(n):
a, b = 0, 1
while a < n:
print(a, end=' ')
a, b = b, a+b
print()
fib(5000)
Output
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181
A recursive implementation
memo = [-1] * 21
memo[0] = 0
memo[1] = 1
print(memo[0], end=' ')
print(memo[1], end=' ')
def fibrec(n):
if(memo[n] == -1):
memo[n] = fibrec(n-2) + fibrec(n-1)
print(memo[n], end=' ')
return memo[n]
fibrec(20)
Output
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765
I have the following text structure. The values below JTT JNX JNA JNO belong to previous line.
9 8 11 56507785 93
JTT JNX JNA JNO
76 98
9 8 60 3269557 58
9 8 53 7269558 150
JTT JNX JNA JNO
132 71 45-7705678
9 8 62 439559 82
I'd like to parse it in order to print the corresponding values in a single line like below:
H1 H2 H3 H4 H5 JTT JNX JNA JNO
9 8 11 56507785 93 76 98
9 8 60 3269557 58
9 8 53 7269558 150 132 71 45-7705678
9 8 62 439559 82
My issue is when I use awk with FS = space (default FS) then it takes JTT as first field and JTT has 9 spaces before, so I think should be use some technique that counts how may spaces are from left until JTT JNX JNA JNO and count number of spaces from beginning until the values below JTT JNX JNA JNO in order to positionate correctly each value. I mean, 76 and 132 below JTT header, 971 below JNX, 98 below JNA and 45-7705678 below JNO.
How can this be done in awk?
$ awk --version
GNU Awk 5.0.0, API: 2.0 (GNU MPFR 4.0.2, GNU MP 6.1.2)
Copyright (C) 1989, 1991-2019 Free Software Foundation.
$ uname -srv
CYGWIN_NT-6.1 3.0.7(0.338/5/3) 2019-04-30 18:08
Thanks in advance.
With GNU awk (which you have) for FIELDWIDTHS:
$ cat tst.awk
BEGIN {
OFS = ","
print "H1", "H2", "H3", "H4", "H5", "JTT", "JNX", "JNA", "JNO"
}
!NF || ($1 == "JTT") { next }
!/^ / {
if (NR>1) {
print rec
}
FS = " "
$0 = $0
$1 = $1
rec = $0
}
/^ / {
FIELDWIDTHS = "12 5 5 *"
$0 = $0
$1 = $1
for (i=1; i<=NF; i++) {
gsub(/^\s+|\s+$/,"",$i)
}
rec = rec OFS $0
}
END {
print rec
}
.
$ awk -f tst.awk file
H1,H2,H3,H4,H5,JTT,JNX,JNA,JNO
9,8,11,56507785,93,76,,98
9,8,60,3269557,58
9,8,53,7269558,150,132,71,,45-7705678
9,8,62,439559,82
$ awk -f tst.awk file | column -s, -t
H1 H2 H3 H4 H5 JTT JNX JNA JNO
9 8 11 56507785 93 76 98
9 8 60 3269557 58
9 8 53 7269558 150 132 71 45-7705678
9 8 62 439559 82
Replace OFS="," with OFS="\t" or otherwise massage to suit...
I have been working on this program for hours and cannot find out how to make the numbers loop around after they hit saturday. They either go way passed it to the right or if i add and endl; they go up and down.
// This is how my output looks like (except they curve around they just go forever to the right:
Number of days: 31
Offset: 0
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
This what you mean?
#include <iostream>
using namespace std;
int main()
{
int i;
for (i=1; i<=31; i++) {
cout << ((i<10) ? " " : "") << i << " ";
if (i%7==0) cout << endl;
}
return 0;
}
Outputs:
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
The % sign is the modulus operator. It gives the remainder of division. So every 7th day divided by 7 is going to have a remainder of zero. That's how you check where to put the line breaks.