1

I used a regular expression to print out only the necessary items in the CSV file. However, I do not know how to add the output value to the data frame. My goal is to put the value x in the existing Data frame column.

import re
path=pd.read_csv('tomy.csv')
s= (path.LN)
for line in s:
x = re.findall('(?i)total\s*\(\d+/\d+\)', line)
if x != 0:
print(x)

output value

'total (10/20)'
'total (15/21)'
...
'total (4/11)'

2 Answers 2

1

Add extra column total and assign the data to the new column.

import re

path = pd.read_csv('tom y. cs v')
s = (path.LN)
total = []
for line in s:
    x = re.findall('(?i)total\s*\(\d+/\d+\)', line)
    if x != 0:
        print(x)
    total.append(x)
path['total'] = total

Note: Don't keep total.append(x) inside if block.

Sign up to request clarification or add additional context in comments.

3 Comments

thanks. output is [ ] and returnes an error ' name 'total' is not defined' any idea what I'm doing wrong?
Did you copy pasted the same code? total was defined above and appending result to total. Please check the note statement in my answer. Please confirm it is working or not?
Great! Don't forget to accept the answer, if it solved. Happy coding!
0

You want to add all the column 'total' from csv to pandas data frame? Try to read all cdv and then filter by that column

path = pd.read_csv('tom y.csv')
path = path['total']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.