xlrd import values, clear formatting, delete entries that are not integers - xlrd

I am trying to import integers from an Excel spreadsheet and create a list, but I need to get rid of both the formatting and other non-integer entries. I'm using xlrd and canopy.
This code:
CBSAlist = []
for value in sheet.col(0):
CBSAlist.append(str(value).split(":")[1])
print CBSAlist
Returns a list with elements that look like this:
"u'43100'", "u'44620'", "u'48020'", "''", "''", "''", "''",
"''", "''", "''", "u'CBSA Code'",
Instead of a list of elements that look like this (which is why I split at the colon):
text:u'49220', text:u'16220', text:u'16940', text:u'21740', text:u'23940',
empty:'', empty:'', text:u'CBSA Code', empty:''

First, in Excel the values were listed as text, not as numbers. I changed that first and got rid of all values in the file that were not numbers. Then I split at the colon and reassigned the values as floats.

Related

loop in json_extract_scalar

The json format I have is in the following form
{"0": {...}, "1":{...}, "2":{..}}
and this goes for a variable number of integers.
I am trying to create a for loop so I can extract all the values at once instead of separately writing a bunch of code with json_extract_scalar.
I wanted to do something like the following
for x in range (0, (some_number)):
json_extract_scalar(my_dataset_column, '$.' + x)

Pandas Replace_ column values

Hello,
I am analyzing the next dataset with this information .
The column ['program_number'] is an object but I want to change it to a integer colum.
I have tried to replace some values but it doesn´t work.
as you can see, some values like 6 is duplicate. like '6 ' and 6.
How can I resolve it? Many thanks
UPDATE
Didn't see 1X and 3X at first.
If you need those numbers and just want to remove the X then:
df["Program"] = df["Program"].str.strip(" X").astype(int)
If there is data in the column which aren't numbers or which shouldn't be converted, you can use pd.to_numeric with errors='corece'. If there are cells which can't be converted, you'll get NaN. Be aware that this will result in floating numbers.
df["Program"] = pd.to_numeric(df["Program"], errors="coerce")
old
You want to use str.strip() here, rather than replace.
Try this:
df1['program_number'] = df1['program_number'].str.strip().astype(int)

Is there an equivalent of an f-string in Google Sheets?

I am making a portfolio tracker in Google Sheets and wanted to know if there is a way to link the "TICKER" column with the code in the "PRICE" column that is used to pull JSON data from Coin Gecko. I was wondering if there was an f-string like there is in Python where you can insert a variable into the string itself. Ergo, every time the Ticker column is updated the coin id will be updated within the API request string. Essentially, string interpolation
For example:
TICKER PRICE
BTC =importJSON("https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&ids={BTC}","0.current_price")
You could use CONCATENATE for this:
https://support.google.com/docs/answer/3094123?hl=en
CONCATENATE function
Appends strings to one another.
Sample Usage
CONCATENATE("Welcome", " ", "to", " ", "Sheets!")
CONCATENATE(A1,A2,A3)
CONCATENATE(A2:B7)
Syntax
CONCATENATE(string1, [string2, ...])
string1 - The initial string.
string2 ... - [ OPTIONAL ] - Additional strings to append in sequence.
Notes
When a range with both width and height greater than 1 is specified, cell values are appended across rows rather than down columns. That is, CONCATENATE(A2:B7) is equivalent to CONCATENATE(A2,B2,A3,B3, ... , A7,B7).
See Also
SPLIT: Divides text around a specified character or string, and puts each fragment into a separate cell in the row.
JOIN: Concatenates the elements of one or more one-dimensional arrays using a specified delimiter.

Python: Remove exponential in Strings

I have been trying to remove the exponential in a string for the longest time to no avail.
The column involves strings with alphabets in it and also long numbers of more than 24 digits. I tried converting the column to string with .astype(str) but it just reads the line as "1.234123E+23". An example of the table is
A
345223423dd234324
1.234123E+23
how do i get the table to show the full string of digits in pandas?
b = "1.234123E+23"
str(int(float(b)))
output is '123412299999999992791040'
no idea how to do it in pandas with mixed data type in column

Dividing the input in python

I'm doing a project where I need to insert coordinates in the console to return a place in a grid. My grid is 10*10 and has numbers in the rows and Letters in the columns.
I want to be able to input something like A1 and for it to be interpreted as "column1, row1"
So far I have got:
def get_coor():
user_input = input("Please enter coordinates (row,col) ? ")
coor = user_input.split(" ")
return coor
But I'm only able to split if I have a space. Is there any other function to help me in this situation?
Strings are iterable in Python.
If you write:
user_input = input("Please enter coordinates (row,col)?")
<input A1>
Then user_input[0] will be A and user_input[1] will be 1.
Therefore, no need for the split :)
Split is used precisely for the use case when there is a space: it returns a list of all the strings between the occurrences of the character given as an argument (in your case a space).