Python join - how to join a data in loop? - python

How can I join a data below,
# Convert Spark DataFrame to Pandas
pandas_df = df.toPandas()
print pandas_df
age name
0 NaN Michael
1 30 Andy
2 19 Justin
My current attempt,
persons = ""
for index, row in pandas_df.iterrows():
persons += str(row['name']) + ", " + str(row['age']) + "/ "
print row['name'], row['age']
print persons
Result,
Michael, nan/ Andy, 30.0/ Justin, 19.0/
But I am after (no slash at the end),
Michael, nan/ Andy, 30.0/ Justin, 19.0

If you want to keep your method of looping through each , then you can simply remove the last / by doing rstrip() on it to strip from the right side. Example -
persons = ""
for index, row in pandas_df.iterrows():
persons += str(row['name']) + ", " + str(row['age']) + "/ "
print row['name'], row['age']
person = person.rstrip("/ ")
print persons
Example/Demo -
>>> person = "Michael, nan/ Andy, 30.0/ Justin, 19.0/ "
>>> person = person.rstrip('/ ')
>>> person
'Michael, nan/ Andy, 30.0/ Justin, 19.0'
But if you really do not want the print row['name'], row['age'] inside the loop, then you can convert this into a generator function and let str.join() handle what you want. Example -
person = "/".join(",".join([str(row['name']), str(row['age'])]) for _, row in pandas_df.iterrows())

I think this will do
persons = []
str_pearsons=""
for index, row in pandas_df.iterrows():
persons.append( str(row['name']) + ", " + str(row['age']))
str_pearsons="/ ".join(persons)

You can achieve this easily in a one liner that will be vectorised:
In [10]:
'/ '.join(df['name'] + ', ' + df['age'].astype(str))
Out[10]:
'Michael, nan/ Andy, 30.0/ Justin, 19.0'

Related

While loop for excel parsing

error message screenshotI'm quite new to Python and I need to create a nested loop for excel parsing. I have a spreadsheet with 4 columns ID, Model, Part Number, Part Description, Year and I need a parser to go through each line and to return in format:
Part Number, Toyota > Model > Year | Toyota > Model > Year etc...
so that part number is returned only once listing all of the multiple fitting models and years.
I was able to achieve the same through the code below but it is not switching to the second part Part Number
import pandas as pd
import xlrd
workbook = pd.read_excel('Query1.xls')
workbook.head()
i = 0
l = int(len(workbook))
a = workbook['Part Number'].iloc[i]
while i < l:
b = 0
c = workbook['Part Number'].iloc[b]
print(a)
while c == a:
#print(c)
print(b, 'TOYOTA >', workbook['Model'].iloc[b], ' > ', workbook['Year'].iloc[b], ' | ', end = ' ')
b = b + 1
print()
i = i + b
Your code gets stuck in an infinite loop, because you do not update the value c as you iterate through the rows. Here's how you could implement this better:
part_number_group = None
for i in range(len(df)): # or `for i, row in df.iterrows():`
part_number = df.loc[i, "Part Number"]
if part_number != part_number_group:
if part_number_group is not None:
print()
print(part_number)
part_number_group = part_number
print(i, 'TOYOTA >', df.loc[i, 'Model'], ' > ', df.loc[i, 'Year'], ' | ', end = ' ')
But instead, you should use groupby, which saves the need to iterate through rows at all:
df["Model-Year"] = df.index.astype(str) + " TOYOTA > " + df["Model"] + " > " + df["Year"].astype(str)
for part_number, group in df.groupby("Part Number"):
print(part_number)
print(*group["Model-Year"], sep=" | ")
Trying to reuse some of your code, you may go over all unique part numbers using a for loop. For loops make it easier to no get stuck in an infinite loop because you specify the start and stop conditions upfront. Then you can query all entries with that same part number and print them with your suggested print function:
import pandas as pd
import xlrd
workbook = pd.read_excel('Query1.xls')
for num in pd.unique(workbook["Part Number"]):
print('\n', num)
part_df = workbook.query("`Part Number` == #num")
for i in range(len(part_df)):
print(i, 'TOYOTA >', part_df['Model'].iloc[i], ' > ', part_df['Year'].iloc[i], ' | ', end=' ')

How can I add a parenthesis before the number 2500?

I'm trying to output the following two lines of text:
The city with the most contracts is:
Chicago (2500 contracts)
this is the code I'm using:
a = 'Chicago'
b = 2500
print('The city with the most contracts is:')
print(a, b, 'contracts)')
the current output is:
The city with the most contracts is:
Chicago 2500 contracts)
How can I add a parenthesis before the number 2500?
I tried adding the parenthesis as a string, as in:
print('The city with the most contracts is:')
print(a, '(', b, 'contracts)')
but it adds an unwanted space between the parenthesis and the number 2500, looks like this:
The city with the most contracts is:
Chicago ( 2500 contracts)
The easiest way is with a formatting string.
print(f'{a} ({b} contracts)')
To handle manually the issue, you'd do
print(a, '(' + str(b), 'contracts)')
But you'd rather use the formatting solutions
print(f'{a} ({b} contracts)')
print('{} ({} contracts)'.format(a, b))
print('%s (%s contracts)' % (a, b))
For Python 3.6+:
print(f"{a} ({b} contracts)")
For a prior version:
print("{a} ({b} contracts)".format(a=a, b=b))
or
print("{} ({} contracts)".format(a, b))
I will not encourage to format using %.
This worked for me using string formatting
a = 'Chicago'
b = 2500
print("The city with the most contracts is:")
print("{} ({} contracts)".format(a,b))
This should work:
a = 'Chicago'
b = 2500
print('The city with the most contracts is:')
print(a + ' (' + str(b) + ' contracts)')
Use the plus sign instead of the comma.
a = "Chicago"
b = 2500
print(a + ' (' + str(b) + ' contracts)')
The output was:
Chicago (2500 contracts)

How to make last element of list finish with a dot and the others with a comma?

I made this list with a for loop that points errors when yoy choose a name. I'd like to know how can I make it so that the last line finishes with '.' and the others finish with ';'.
while True:
if len(errors_list) != 0:
print("Your name has thesse errors::")
for i in errors_list:
print(" " + str(errors_list.index(i) + 1) + "- " + i + ".")
print("Try again.")
errors_list.clear()
name = input("My name is ").title()
choose_name(name)
else:
print("Nice to meet you, " + fname + " " + sname + ".")
break
Result when I type a name like '--- ':
Your name has these errors:
1- It has no letters.
2- It has symbols.
3- The last letter is a space.
Try again.
My name is
I'd like to make it so that 1 and 2 finish with ';' and 3 with '.'. Thanks!
All the existing solutions so far seem pretty poor, this is as print is expensive to call.
errors_list.index(i) runs in O(n) time making your solution run in O(n^2) time. You can improve this, to O(n) time, by using enumerate.
You can also think of what you're doing simply as concatenating values of a list and adding a period.
I would use:
errors = [f' {i}- {error}' for i, error in enumerate(errors_list, 1)]
print(';\n'.join(errors) + '.')
Extending Roman Perekhrest's answer, enumerate has an optional parameter start:
errors_list = ['It has no letters', 'It has symbols', 'The last letter is a space']
for i, err in enumerate(errors_list, start=1):
print("\t{}- {}{}".format(i, err, ';' if i < len(errors_list) else '.'))
additionaly with Python 3.6+ you can use f-strings instead of format:
errors_list = ['It has no letters', 'It has symbols', 'The last letter is a space']
for i, err in enumerate(errors_list, start=1):
print(f"\t{i}- {err}{';' if i < len(errors_list) else '.'}")
Instead of:
for i in errors_list:
print(" " + str(errors_list.index(i) + 1) + "- " + i + ".")
do
s = len(errors_list)
for e, i in enumerate(errors_list):
ending = ";" if e + 1 < s else "."
print(" " + str(errors_list.index(i) + 1) + "- " + i + ending)
EDIT:
to those jumping to the gun - OP did write in a title comma, but he used semicolon (;) twice (!) in a question itself.
Simply with enumerate function:
errors_list = ['It has no letters', 'It has symbols', 'The last letter is a space']
...
for i, err in enumerate(errors_list):
print(" {}- {}{}".format(i+1, err, ';' if i+1 != len(errors_list) else '.'))
The crucial loop will output:
1- It has no letters;
2- It has symbols;
3- The last letter is a space.

How to repeat string message without use asterisk, loop, array, import

I get a quiz about programming python.
Input: 3 lines of any string. (only 3 lines)
Output: 3 * 5 = 15 lines of string which repeat 3 lines of input 5 rounds
** But this quiz has restricted word: import for while * . sep if else elif list set tuple dict [] {} lambda map filter
I already try it by use asterisk character to repeat string but this is restricted word. It cannot submit.
STRING_A = input()
STRING_B = input()
STRING_C = input()
STRING_RESULT = STRING_A + "\n" + STRING_B + "\n" + STRING_C + "\n"
print(STRING_RESULT * 5)
Example
Input:
man
in
middle
Output:
man
in
middle
man
in
middle
man
in
middle
man
in
middle
man
in
middle
Thanks for your helping.
Given your restrictions, recursion sounds like a good approach. Give this a shot!
def repeater(a,n):
n <= 0 and exit(0)
n == 1 and print(a)
print(a)
return(repeater(a,n-1))
STRING_A = input()
STRING_B = input()
STRING_C = input()
STRING_RESULT = STRING_A + "\n" + STRING_B + "\n" + STRING_C
repeater(STRING_RESULT, 5)
Output:
man
in
middle
man
in
middle
man
in
middle
man
in
middle
man
in
middle

How to dynamically print list values?

This Code Works Fine.....But it's like static.
I don't know what to do to make it work in dynamic way?
I want:-
When user inputs 3 number of cities it should give
a="You would like to visit "+li[0]+" as city 1 and " +li[1]+ " as city
2 and "+li[2]+" as city 3 on your trip"
Similaraly when input is 5 cities it should go to 5 times
li = []
global a
number_of_cities = int(raw_input("Enter Number of Cities -->"))
for city in range(number_of_cities):
li.append(raw_input("Enter City Name -->"))
print li
a="You would like to visit "+li[0]+" as city 1 and " +li[1]+ " as city 2 and "+li[2]+" as city 3 on your trip"
print a
a = a.split(" ")
print "\nSplitted First Sentence looks like"
print a
print "\nJoined First Sentence and added 1"
index = 0
for word in a:
if word.isdigit():
a[index] = str(int(word)+1)
index += 1
print " ".join(a)
You should do something like this
a = 'You would like to visit ' + ' and '.join('{0} as city {1}'.format(city, index) for index, city in enumerate(li, 1)) + ' on your trip'
You can build the string with a combination of string formatting, str.join and enumerate:
a = "You would like to visit {} on your trip".format(
" and ".join("{} as city {}".format(city, i)
for i, city in enumerate(li, 1)))
str.join() is given a generator expression as the iterable argument. The curly braces ({}) in the strings are replacement fields (placeholders), which will be replaced by positional arguments when formatting. For example
'{} {}'.format('a', 'b')
will produce the string "a b", as will
# explicit positional argument specifiers
'{0} {1}'.format('a', 'b')
create another for loop and save your cities to an array. afterwords, concat the array using "join" and put put everything inside the string:
cities = []
for i in range(0,len(li), 1):
cities.append("%s as city %i" % (li[i], i))
cities_str = " and ".join(cities)
a = "You would like to visit %s on your trip" % (cities_str) # edited out the + sign

Categories