Merge two consecutive rows based on condition [closed] - python

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I want to merge two consecutive rows. If there is "SMN" in Pos column followed by B-PER then merge the two rows. If SMN is not followed by B-PER then don't merge
This is the desired result that I want to achieve

for i in range(1, df.shape[0]):
if df['Pos'][i] == 'B-PER' and df['Pos'][i-1] == 'SMN':
newString = df['names'][i - 1].strip() + " " + df['names'][i].strip()
df['Pos'][i-1] = newString
df = df.drop(index=i)
This should work. Not sure the datatype of Arabic strings, so you might have to play with python datatypes to get this to work.

Related

Go back to the original number after the sum of values ​between two lists [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 11 months ago.
Improve this question
I wrote this code to sum a number and its reverse:
#sum the multiple of the first number and its reversed
zipped_lists = zip(primo, reversedList)
res = [x + y for (x, y) in zipped_lists]
print (res)
#search common values in "res" and "secondo"
common_list = set(res).intersection(secondo)
Now that I isolated significant numbers, I need to return to the original number listed in primo. I've no idea how to do that. Please, help me :(
You need to keep track of x against its corresponding value of x + y. Instead of checking for the presence in secondo after you create res, you could do the filtering in a single comprehension.
res = [
x
for x, y in zip(primo, reversedList)
if (x + y) in secondo
]

convert multiple columns vertical text to horizontal [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I tried these, but its only for one column.
using notepad++ and python. Any solution that can do for multiple columns of text?
# Input:
tim
oso
d n
a d
y a
y
# Output:
today
is
monday
You can do this fairly easily with a loop, something like:
s = "tim\noso\nd n\na d\ny a\n y"
bits = s.split('\n')
lines = ['' for i in range(0, len(bits[0]))]
for bit in bits:
for i in range(0, len(bit)):
if bit[i] != ' ':
lines[i] += bit[i]
'\n'.join(lines)

String Manipulation in Dataframe [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Hey guys I have a quick question regarding the string manipulation in pandas dataframe.
Suppose we have 2 columns looks like this:
Question:
How I can keep only the string part for each cell and delete the [' ']?
Thank you so much for your help! I am looking forward to hearing your brilliant idea!
Please use regex to replace all non alphanumeric characters
print(df)
State City
0 ['AK'] ['Yakutat']
1 ['AK'] ['Apache']
Solution
df=df.replace(regex='[^\w]',value='')
print(df)
State City
0 AK Yakutat
1 AK Apache
Depends if the values in each of your cells are strings with brackets "['AK']" or actual lists: ['AK'].
If they are strings with brackets on either side, we can strip bracket characters from both sides:
df["State"] = df["State"].str.strip("[]")
df["City"] = df["City"].str.strip("[]")
If they are lists with you can join them with a comma to turn them into a string
df["State"] = df["State"].str.join(", ")
df["City"] = df["City"].str.join(", ")
You can do the following:
df['City']=df['City'].apply(lambda x: x[2:-2])
df['State']=df['State'].apply(lambda x: x[2:-2])

Put a set of characters into list in python [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
Essentially I want to put characters inside a list so I can make the for loop work.
Example characters (supposedly listed individually vertically. not like this showing as one line)
N/A
N/A
None
Test
Dev
I want to put these characters into a list like below
a = [N/A, N/A, None, Test, Dev]
Reason is when I use for loop into these characters (let's say there are all inside "test_report". For loop is not working because it is reading the "test_report" as like this:
N
/
A
N
/
A
N
o
n
e
count = 0
for i in test_report:
if i == "Test":
count += 1
return count
Try this...
with open('test_report.txt', 'r') as f:
test_arr = f.read().split()
print(test_arr)
Considered your test_report.txt like this...
N/A
N/A
None
Test
Dev

How to count frequencys in python? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I had a file called 'words.txt' which contains things such as. #+=%£%= and i need to go through and count each symbol without allowing any duplicates and then print my answer. for example it should look like this when printed:
# : 1
+ : 1
= : 2
% : 2
£ : 1
I know I need to use a for loop in this and that I need to use a set so It doesnt't allow any duplicates but how would I do it? Thanl you
A set isn't so useful here as it doesn't have a place to store the counts
from collections import Counter
with open("words.txt") as fin:
c = Counter(fin.read())
for item in c.items():
print("{} : {}".format(*item))
Use python dictionary:
symbols = {}
for c in string:
if c not in symbols:
symbols[c] = 0
symbols[c] += 1
Look up dictionary.
Don't want to post more as it would only spoil the exercise.

Categories