Iterating in dataframe and insert on text - arrays

There is a DataFrame loaded in pandas with size m*n, m can be big compare with columnes n which are values from 2 to 20.
each value from m*n has to be add to expecific text, it means that text between any value is constant.
I tried with For and If nested sentences, no good result how to make step from df.iloc[0,0] to df.iloc[m,n] and insert in text.
textA + df.iloc[0,0] + textB + df.iloc[0,1] + .... + textX + df.iloc[0,n]
textA + df.iloc[1,0] + textB + df.iloc[1,1] + .... + textX + df.iloc[1,n]
.
.
.
textA + df.iloc[m,0] + textB + df.iloc[m,1] + .... + textX + df.iloc[m,n]
I have 2 files, one include textA textB ... textX Second file is csv type where pandas dataframe is generated.
With dataframe and text prepare array above.
thanks for any tip.

Not exactly clear on your requirement but takae a look if the following code is able to help you iterate between df.iloc[0,0] to df.iloc[m,n] while adding information from your text file (in this case a text variable)
# i am using a dummy variable for the text data as i am not sure how your data look like
text = ['textA', 'textB', 'textC']
new_text = []
# using a nested for loop to iterate
for row in range(len(df)):
for col in range(len(df.columns)):
new_text.append(text[col] + str(df.iloc[row, col]))

Related

What is an efficient algorithm to input 4B integers to a text file

Let's say I want to write 1,2,3,4....up to 4.096B in a text file. What would be a time efficient way to do it. Just doing it sequentially is taking a long time. So wondering if there's a distributed way.
Thanks to all your comments on my question. It helped me solve this problem in a reasonable amount of time. Here's what I did -
Create a file using Excel to create a million integers from 0 - 1000000
Upload this file in Hadoop
Write a Hive query with 4296 lines like below -
a0 = SELECT IPDecimal + (100000 * 1) + 1 AS IPDecimal FROM #file;
a1 = SELECT IPDecimal + (100000 * 2) + 1 AS IPDecimal FROM #file;
.
.
.
a4295 = SELECT IPDecimal + (100000 * 4295) + 1 AS IPDecimal FROM #file;
Output the result of each SELECT statement above in a separate file and then consolidate the integers in the 4296 files in one single file

How to sum columns in SQL Server?

I am trying to create a column that is the total value of column A,B,C,D,E.
select,[TVIncome] a
,[XIncome] b
,[ZIncome] c
,[DINCOME] d
,[OIncome] e
sum(a,b,c,d,e) as total
The error I get when doing the above sum is :"The sum function requires 1 argument(s)."
The total above does not work. Also, if I just include the the proper names, XIncome and the rest,it still does not work. How do I do it?
SUM() adds each value in a column, to give a single value
+ adds each value in two columns together to give a column
It sounds like you are after a + b + c + d + e AS total,
or possibly SUM(a + b + c + d + e) AS total if you are after 1 value
Assuming you want a sum for those columns for each row
SELECT (TVIncome + XIncome + ZIncome + DIncome + OIncome) as TotalIncome
FROM Table
If you want a sum of sums, which will equal one row:
SELECT SUM(TVIncome + XIncome + ZIncome + DIncome + OIncome) as TotalIncome
FROM Table

Need some help parsing a string in C

Using fgets, I have read in a line from a text file. The line may be something like this:
# O^6+ + H -> O^5+ + H^+
Or it may be this:
# Mg^12+ + H -> Mg^11+ + H^+
or this:
# Ne^10+ + He -> Ne^9+ + He^+
Or a multitude of other possibilities.
I am trying to extract the ion, the charge and the atom terms from the string.
I tried something like this:
sscanf(line,"# %2s^%d+ + %2s",cs->ION,&(cs->Z),cs->ATOM);
I also tried this:
sscanf(line,"# %[^^]s^%d+ + %2s",cs->ION,&(cs->Z),cs->ATOM); Because I was picking up the '^' character.
I just can't seem to get this to work for every case. Any suggestions are appreciated.
Your try with the format string
"# %[^^]s^%d+ + %2s"
was almost right, except that after the %[^^] there has to be no s, i. e.
"# %[^^]^%d+ + %2s"
works.

Convert an array of timestamps to seconds in Matlab

I'm new to SO and Matlab so please excuse any transgressions.
I'm trying to convert a seemingly simple array of timestamp strings to an equivalent array of seconds.
I wrote a this function:
% Function to calculate seconds from a timestamp in the following format:
% ddd hh:mm:ss.SSSS (example: 123 12:59:00.9999)
function a = TimestampToS(stamp)
% Uses the "named tokens" facility of MATLAB's "regexp" function.
expr = ['(?<ddd>\d+)' ... % ddd
' ' ... % Space " " separator
'(?<hh>\d+)' ... % hh
':' ... % Colon ":" separator
'(?<mm>\d+)' ... % mm
':' ... % Colon ":" separator
'(?<ss>\d+)' ... % ss
'.' ... % Dot "." separator
'(?<SSSS>\d+)']; % SSSS
parsedStamp = regexp(stamp, expr, 'names');
a = (str2double(parsedStamp.ddd) * 86400) + ...
(str2double(parsedStamp.hh) * 3600) + ...
(str2double(parsedStamp.mm) * 60) + ...
(str2double(parsedStamp.ss)) + ...
(str2double(parsedStamp.SSSS) * 0.0001);
It works great for an individual string:
>> TimestamptoS('123 12:59:00.9999')
ans =
1.067394099990000e+007
But if I try to use a cell array I get:
Attempt to reference field of non-structure array.
How can I get an array of seconds? I have tried all kinds of conversions of the input data and "parsedStamp" but nothing works. I don't understand Matlab or its matrix notation well enough. Any help gratefully received!
PS This is not a regexp question, no replies about regexp please!
You can do it very easily without modifying your function, by using cellfun. This essentially extracts each cell of the cell array and passes it to your function.
>> cellArray = {'123 12:59:00.9999','130 12:59:00.9999'}; % for example
>> cellfun(#TimestampToS,cellArray)
ans =
1.0e+007 *
1.067394099990000 1.127874099990000

What will be the DFA for the regular expression 0(0+1)*0+1(0+1)*1?

This is the DFA i have drawn-
Is it correct?
I am confused because q4 state has 2 different transitions for same input symbol which violates the rule of DFA, but I can't think of any other solution.
Your DFA is not correct.
your DFA is completely wrong so I don't comment
DFA for RE:
0(1 + 0)*0 + 1(1 + 0)*1
Language Description: if string start with 0 it should end with 0 or if string start with 1 it should end with 1. hence two final states (state-5, state-4).
state-4 : accepts 1(1 + 0)*1
state-5 : accepts 0(1 + 0)*0
state-1 : start state.
DFA:
EDIT :
+ Operator in Regular Expression
(0 + 1)* = (1 + 0)* that is any string consist of 1s and 0s, including Null string ^.
Here + means Union if it appear between two RE: and A U B = B U A (similarly)=> (0 + 1) = (0 + 1) .
meaning of plus + depends on syntax it appears in: If expression is a+ (+ is superscripted) this means one of more as, and If a+b then + means Union operation either a or b.
a+ : { a, aa, aaa, aaa.....} that is any number of a string in language with length > 1.
I think you should start with 0 first
0(1 + 0)*0 + 1(1 + 0)*1

Resources