I am trying to understand the intuition behind the following line of code.
I know that it is removing double quotes from the column header within a dataframe, although can anyone please help me understand how it is doing that?
df.columns = [col[1:-1] for col in df.columns]
Thanks
df.columns = ... is the part of line that assigns the list on the right hand side to the columns.
Then the right hand side is a list comprehension, meaning it can be understood like a for loop.
Then, in python, a string is an array of characters. for col in columns mean you iterate over each string in the list of columns. Each col is an array. If that string has quotes, then it looks like "xxxx". So the first and the last elements of the array are quotes.
col[1:-1] is the way to slice the array from the second element to the one before last.
So when you put all these things next to each other, in your case you end up removing quotes.
This is to take 2nd index to 2nd last index in a column names string for each column
Example:-
If column name string is of size less than 3, then it gives you empty string as column name
Example -
Related
I am using pd.read_clipboard() function to get an excel table that doesnt have column names as first row . The dataframe returned has first row as column labels. How to fix that.
I would like results to be
and not this
Though not showing up on help for read_clipboard() function , passing read_clipboard(names=['c1','c2']) where c1 and c2 are the column names fixes the read_clipboard() function to not treat first row as column names i.e provide column names to avoid having the function treat first row as column names
I know the title sounds kinda repetitive but I couldn't find this answer on microsoft docs, reddit or stack overflow.
So I want to convert a column (static, string) into a dynamic array and i want to match the values of that array to another column in another table.
So let's say there is a tab A with col 1 and tab B with col 2 and i basically want to match each element of col 1 with the whole of col 2 i.e. column 1's first value will be matched against all the values of col 2 and then col 1's 2nd value will be matched against all the values of col 2 and so on. After hours of google-fu i barely found anything that does this type of checking in KQl and found out that only way is to use a dynamic array and then KQL iterates over that array and matches the way i want it to match. But then again i can't convert col 1 to a dynamic array and use that array to match against the col 2 of tab B. i have used make_list and similar aggregation functions but to no help.
So if anyone knows how to do this i'll be really grateful! Thanks!
Converting a string into a dynamic array can be done using the todynamic() function.
In OpenRefine, I'm trying to increase the value of every number in a column by 1.
The GREL expression sum([value],1) gives me Error: sum expects an array of numbers.
I guess I don't know how to produce an array of numbers. When I use a different function on the same column, such as tan([value]), I get the result I want.
I think you misunderstood the use of sum(). If you just want to add 1 to each cell, just use value + 1.
Make sure, however, that your column contains numbers (in green) and not strings (in black). If in doubt, use toNumber(value) + 1 instead.
The sum() function allows to add all the numbers contained in an array, for instance sum([1,2,3,4]) = 10, but you have no array if each cell of your column contains a unique number.
For the following table,
I want to look up the value in col C. Since the values in col A and col B are not unique, VLOOKUP fails. For example, VLOOKUP(1,table,3) returns 5 and never 1.
However, the combinations of cols A & B are unique. For example, 1blah = 5, while 1foo = 1.
How can I use the combination of cols A & B as a unique key to return the corresponding value in col C?
I'm not sure if this should be implemented with worksheet functions or a custom VBA function. I tried using CONCATENATE to generate the unique key, but this did not work because this results in combinations of numbers and strings.
You can use an array formula:
=INDEX($C$1:$C$7,MATCH("1foo",$A$1:$A$7 & $B$1:$B$7,0))
just select in example D1, enter formula in formula bar and press CTRL+SHIFT+ENTER to evaluate it
The way I usually do it is by concatenating the values separated by a pipe character (|). See the formula in the screenshot below.
Then you can vlookup using the concatenated key.
=VLOOKUP("1|foo",$C$1:$D$7,2,FALSE)
You can use VLOOKUP if your lookup value is a concatenation of your two or more key fields (A2&B2). Then, add a first sorted column in your LUT sheet with the array that is the concatenation of the same key fields. Viola.
I am having a value "90672.40 91796473.18" in one variable.
I need to store the "90672.40" in an another variable and remaining in another variable.
and also I need to retrieve a numbers from the 40th position to end.
kindly help me..
for your information,
I am having a variable named lsline.
Value of lsline is:
lsline = 15-OCT-08 OTHERS 90672.40 91796473.18
i need to retrieve a number from 40th position, but i dont want the numbers after the space, i just want "90672.40" to store in a another variable.
Use Substring to get part of a string if you don't want to use Split. The following returns 8 characters starting from number 40:
lsline.Substring(40, 8)
Just split into an array
Dim values = Isline.Split(" ")
Dim number = CDec(value[2])