Reputation: 149
I want to read each head of each column (A=77.34, B=78.28, C=85.44 and D=92.46 in this case) and to save the DataFrame. If all these heads are greater than 65, save as csv file to specific folder (High). If low than 55, save as csv to specific folder (Low). I can't see any csv file in the folder (High in this case), and i don't understand where is my problem. I am using Python27.
My program:
import pandas as pd
import numpy as np
import os
import csv
df = pd.DataFrame({'A': [77.34, 44.09, 44.15, 43.61],
'B': [78.28, 46.28, 46.00, 46.03],
'C': [85.44, 41.89, 42.15, 42.65],
'D': [92.46, 42.22, 42.55, 42.02]
}, index=pd.Index(range(4), name='idx'))
X1=df.iloc[0,0]
X2=df.iloc[0,1]
X3=df.iloc[0,2]
X4=df.iloc[0,3]
def Rho (df):
if (X1>65 and X2>65 and X3>65 and X4>65):
df.to_csv('High.csv')
path = 'D:\My_Path\High'
extension = 'csv'
os.chdir(path)
elif (X1<55 and X2<55 and X3<55 and X4<55):
df.to_csv('Low.csv')
path = 'D:\My_Path\Low'
extension = 'csv'
os.chdir(path)
else:
print("Ignore")
Rho (df)
Upvotes: 3
Views: 19468
Reputation: 21
using df.to_csv(path="xxxx")
does not work anymore.
According to the pandas documentation you either us:
from pathlib import Path
filepath = Path('folder/subfolder/out.csv')
filepath.parent.mkdir(parents=True, exist_ok=True)
df.to_csv(filepath)
OR
import os
os.makedirs('folder/subfolder', exist_ok=True)
df.to_csv('folder/subfolder/out.csv')
Thanks!
Upvotes: 2
Reputation: 3001
You're using to_csv
the wrong way. You can pass it the full path, including the dir, filename and extension with the path
argument. You also don't need to change your current dir with os.chdir(path)
.
See the docs.
...
def Rho (df):
if (X1>65 and X2>65 and X3>65 and X4>65):
df.to_csv(path='D:\My_Path\High.csv')
elif (X1<55 and X2<55 and X3<55 and X4<55):
df.to_csv(path='D:\My_Path\Low.csv')
else:
print("Ignore")
...
Upvotes: 6