Split a numeric string to a list of integers - python

I have fetched a list using pandas, but the numeric is like a numeric string. I am trying to convert it to a list of integers.
excel_frame = read_excel(args.path, sheet_name=1, verbose=True, na_filter=False)
data_need = excel_frame['Dependencies'].tolist()
print(data_need)
intStr = data_need.split(',')
map_list = map(int, intStr)
print(map_list)
I am getting the following error.
$python ExcelCellCSVRead.py -p "C:\MyCave\iso\SDG\Integra\Intest\first.xlsx"
Reading sheet 1
['187045, 187046']
Traceback (most recent call last):
File "ExcelCellCSVRead.py", line 31, in <module>
intStr = data_need.split(',')
AttributeError: 'list' object has no attribute 'split'
The target output must be like this -> [187045, 187046]. The current output is coming out like this ->['187045, 187046']
I am pretty sure I have followed suggested approach to resolve the issue, yet it is throwing error.
Regards
data_need

The problem is:
data_need = excel_frame['Dependencies'].tolist()
returns a list. So you can't split it further.
Change your existing code to this:
intStr = data_need[0].split(',') ## if you have only 1-element in data_need
map_list = list(map(int, intStr))
print(map_list)
Tested on your sample:
In [1000]: data_need = ['187045, 187046']
In [1001]: intStr = data_need[0].split(',')
In [1002]: map_list = list(map(int, intStr))
In [1003]: print(map_list)
[187045, 187046]

Related

How do i convert a modified list of The Alphabet into a number?

i want to convert the sentence from variable (salam) into numbers. The conversion table is like a modified alphabet just like in (char2).
My expected output is a 3x3 matrix, inside is the converted number from(salam) using (char2)
salam = "APAKABARBROOOOO"
salam = salam.lower
output = []
char2 = [' ','a','b','c','d','e','f','g','h','i','j','k','l',
'm','n','o','p','q','r','s','t','u','v','w','x','y','z','.',',']
i = 0
while i <= 15:
np.array(char2.index(salam[i]))
i = i+1
and the output is
Traceback (most recent call last):
File "C:\Users\dragg\testing funtction losaot[sn[ga\main.py", line 12, in <module>
np.array(char2.index(salam[i]))
TypeError: 'builtin_function_or_method' object is not subscriptable
here is the image for clarity
The problem is from salam.lower. It should be salam.lower().
Without the () you are just referencing the .lower object.

odd TypeError: 'int' object is not iterable

so i was coding some problems in python in this page
https://www.codewars.com/kata/airport-arrivals-slash-departures-number-1/train/python
the code work fine on my computer but when i update it, i came across this bug.
note that its python 3.4.3
def flap_display(lines, rotors):
baseString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ ?!##&()|<>.:=-+*/0123456789"
res = []
baseLen = len(baseString)
lineLen = len(lines)
sLen = len(rotors)
carrier = 0
for item in range(0 , sLen):
if (item < lineLen):
carrier =carrier + rotors[item]
tmp = baseString.index(lines[item])
tmp = tmp + carrier
tmp = tmp % baseLen
res.append( baseString[tmp] )
resS = ''.join(res)
return resS
print (flap_display("CAT", [1,13,27]))
all the website gave me is this:
Traceback:
in
in flap_display
TypeError: unsupported operand type(s) for +: 'int' and 'list'
Now i want to know if my code is incorrect or its just the site being buggy.
Problem is solved! Thank to mr.kuro
sum requires an iterable: a sequence of items, such as a list. You gave it a single integer. If you want to add up all the integers in rotors, you can do that outside of a loop, with
carrier = sum(rotors)
More to your code, just add up the items you wanted:
carrier = sum(rotors[:lineLen])
This adds the first lineLen elements of rotors, allowing you to get rid of that pesky if statement.
Can you adapt the rest of the loop logic to take proper advantage of that?
Thr traceback should be like below:
Traceback (most recent call last):
File "test1.py", line 17, in
print (flap_display("CAT", [1,13,27]))
File "test1.py", line 10, in flap_display
carrier =carrier + sum(rotors[item])
TypeError: 'int' object is not iterable
And, as the traceback says, in line
carrier =carrier + sum(rotors[item])
rotors[item] will apparently be an int, so you can't call sum on it, hence the Error.
Replace the above line with:
carrier = carrier + rotors[item]
Or, just skip the loop, and do:
carrier = sum(rotors)
It should be okay now.

Struggling to understand why float() is giving this error

Here is a piece of the dataset:
18,8,307,130,3504,12,70,1,chevrolet
15,8,350,165,3693,11.5,70,1,buick
18,8,318,150,3436,11,70,1,plymouth
16,8,304,150,3433,12,70,1,amc
17,8,302,140,3449,10.5,70,1,ford
15,8,429,198,4341,10,70,1,ford
14,8,454,220,4354,9,70,1,chevrolet
14,8,440,215,4312,8.5,70,1,plymouth
Here is the code:
data = sc.textFile("hw6/auto_mpg_original.csv")
records = data.map(lambda x: x.split(","))
hp = float(records.map(lambda x: x[3]))
disp = np.array(float(records.map(lambda x: x[2])))
final_data_1 = LabeledPoint(hp, disp)
Here is the error:
Traceback (most recent call last):
File "/home/cloudera/Desktop/hw6.py", line 41, in <module>
hp = float(records.map(lambda x: x[3]))
TypeError: float() argument must be a string or a number
This seems basic, but i'm really having trouble tracking down a solution to this.
Check the type of records.map() probably an RDD. You can apply the float() in the map(), e.g.:
hp = records.map(lambda x: float(x[3]))
But you will need to .collect() the results before using it, e.g.:
hp = records.map(lambda x: float(x[3])).collect()
disp = np.array(records.map(lambda x: float(x[2])).collect())
There is a problem with the input from the CSV, the column is either empty or containing non numeric value

TypeError: '_io.TextIOWrapper' object is not subscriptable

The main function that the code should do is to open a file and get the median. This is my code:
def medianStrat(lst):
count = 0
test = []
for line in lst:
test += line.split()
for i in lst:
count = count +1
if count % 2 == 0:
x = count//2
y = lst[x]
z = lst[x-1]
median = (y + z)/2
return median
if count %2 == 1:
x = (count-1)//2
return lst[x] # Where the problem persists
def main():
lst = open(input("Input file name: "), "r")
print(medianStrat(lst))
Here is the error I get:
Traceback (most recent call last):
File "C:/Users/honte_000/PycharmProjects/Comp Sci/2015/2015/storelocation.py", line 30, in <module>
main()
File "C:/Users/honte_000/PycharmProjects/Comp Sci/2015/2015/storelocation.py", line 28, in main
print(medianStrat(lst))
File "C:/Users/honte_000/PycharmProjects/Comp Sci/2015/2015/storelocation.py", line 24, in medianStrat
return lst[x]
TypeError: '_io.TextIOWrapper' object is not subscriptable
I know lst[x] is causing this problem but not too sure how to solve this one.
So what could be the solution to this problem or what could be done instead to make the code work?
You can't index (__getitem__) a _io.TextIOWrapper object. What you can do is work with a list of lines. Try this in your code:
lst = open(input("Input file name: "), "r").readlines()
Also, you aren't closing the file object, this would be better:
with open(input("Input file name: ", "r") as lst:
print(medianStrat(lst.readlines()))
with ensures that file get closed.
basic error my end, sharing in case anyone else finds it useful. Difference between datatypes is really important! just because it looks like JSON doesn't mean it is JSON - I ended up on this answer, learning this the hard way.
Opening the IO Stream needs to be converted using the python json.load method, before it is a dict data type, otherwise it is still a string. Now it is in a dict it can be brought into a dataFrame.
def load_json(): # this function loads json and returns it as a dataframe
with open("1lumen.com.json", "r") as io_str:
data = json.load(io_str)
df = pd.DataFrame.from_dict(data)
logging.info(df.columns.tolist())
return(df)

Rename a file using variables in the program - Python

I want to rename a file called decon.out using two variables in my program. So far I have
gwf = input ("Enter value: ")
myList = os.listdir('.')
for myFile in myList:
if re.match("^HHEMQZ", myFile):
numE = myFile
elif re.match("^HHNMQZ", myFile):
numN = myFile
else:
den = myFile
os.rename('decon.out', 'RF'+gwf+''+numE+'')
For example, gwf = 2.5 and numE = HHEMQZ20010101
I would then want decon.out to be renamed as RF2.5HHEMQZ20010101 where RF will always be the same.
Currently when I run the script I get an error:
Traceback (most recent call last):
File "RunDeconv.py", line 77, in <module>
os.rename('decon.out', 'RF'+gwf+''+numE+'')
TypeError: cannot concatenate 'str' and 'float' objects
Any suggestions?
Use raw_input() instead, input() interprets the input values as Python code turning your 2.5 input into a float number.
About the error: in the string concatenation
'RF'+gwf+''+numE+''
all the members must be strings.
You can use
type(gwf)
type(numE)
to check which is a number.
You then just need to
str(gwf)
or
str(numE)
depending on which may be the case. Or probably both gwf and numE need the str() treatment, so your last line of code should look like this:
os.rename('decon.out', 'RF'+str(gwf)+''+str(numE)+'')

Categories