1

I have 2 script files in python

1, This one keeps only numbers and remove characters that are not.

import pandas as pd
import re
from re import sub


data = pd.read_csv("C:/Path_to_csv_file.csv")
data.columns=["var1", "var2", "var3"]


var1_list = list(data.var1)
var2_list = list(data.var2)
var3_list = list(data.var3)


print("var1")    
for i in (data.var1):
    a=re.sub("[^\d\.]", "", i)
    float(a) 
    print(a) 
print("\n\n")

print("date")    
for i in (data.date):
    a=re.sub("[^\d\.]", "", i)
    float(a) 
    print(a) 
print("\n\n")

print("var3")    
for i in (data.var3):
    a=re.sub("[^\d\.]", "", i)
    float(a) 
    print(a) 
print("\n\n")

2, This one keeps 2 digits before the period (from left)

import pandas as pd
import re
from re import sub


data = pd.read_csv("C:/Path_to_csv_file.csv")
data.columns=["var1", "var2", "var3"]


var1_list = list(data.var1)
var2_list = list(data.var2)
var3_list = list(data.var3)


print("var1")
for i in (var1_list):
    formatted = format(i, '02.0f')
    print(formatted[-2:])
print("\n\n")

print("var2")
for i in (var2_list):
    formatted = format(i, '02.0f')
    print(formatted[-2:])
print("\n\n")

print("var3")
for i in (var3_list):
    formatted = format(i, '02.0f')
    print(formatted[-2:])
print("\n\n")

How to combine them and get the final result print directly in a excel csv file (to the pandas data file or a new file, both ways are fine).

When I combined it I got trouble with the data type. Before the script 1 every result is understood as string. The result looks like this w[var1] = -1282.982

1 Answer 1

1

Hi I suggest that you make your .csv file to a pandas dataframe and then use the method pd.concat(....). Hope this helps!

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

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.