Python - configuration file - python-3.x

I have code:
import matplotlib.pyplot as plt
from configparser import ConfigParser
cfg = ConfigParser()
cfg.read('file.cfg')
plt.plot([1, 10],[2, 2], color_4, ls = "dashed")
plt.xlim(1,10)
plt.ylim(1,4)
plt.savefig('image.pdf')
and I would like control it by configuration file:
[a]
color_4 = c = 'silver'
What is wrong please? It gives an error:
NameError: name 'color_4' is not defined

I guess you need to get the value in this way to get the value of color_4:
cfg['a']['color_4']
from configparser import ConfigParser
cfg = ConfigParser()
cfg.read('file.cfg')
plt.plot([1, 10],[2, 2], cfg['a']['color_4'], ls = "dashed")
plt.xlim(1,10)
plt.ylim(1,4)
plt.savefig('image.pdf')
Ref: ConfigParser

Related

Having issues with sys.argv()

I'm new to python programming and trying to implement a code using argv(). Please find the below code for your reference. I want to apply filter where Offer_ID = 'O456' with the help of argv().
Code:
-----
import pandas as pd
import numpy as np
import string
import sys
data = pd.DataFrame({'Offer_ID':["O123","O456","O789"],
'Offer_Name':["Prem New Ste","Prem Exit STE","Online Acquisiton Offer"],
'Rule_List':["R1,R2,R4","R6,R2,R3","R10,R11,R12"]})
data.loc[data[sys.argv[1]] == sys.argv[2]] # The problem is here
print(data)
With this statement I'm getting the output -> "print(data.loc[data['Offer_ID'] =='O456'])"
but I want to accomplish it as shown here "data.loc[data[sys.argv[1]] == sys.argv[2]]" .
Below is the command line argument which I'm using.
python argv_demo2.py Offer_ID O456
Kindly assist me with this.
I'm a little confused as to what the issue is, but is this what you're trying to do?
import pandas as pd
import numpy as np
import string
import sys
data = pd.DataFrame({'Offer_ID':["O123","O456","O789"],
'Offer_Name':["Prem New Ste","Prem Exit STE","Online Acquisiton Offer"],
'Rule_List':["R1,R2,R4","R6,R2,R3","R10,R11,R12"]})
select = data.loc[data[sys.argv[1]] == sys.argv[2]] # The problem is here
print(select)

ax = plt.axes(projection=ccrs.Mercator()) give an error

I have the following program:
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
birddata = pd.read_csv("bird_tracking.csv")
bird_names = pd.unique(birddata.bird_name)
plt.figure(figsize=(10,10)
ax = plt.axis(projection=ccrs.Mercator())
ax.set_extent((-25.0,20.0,52.0,10.0))
ax.add_feature(cfeature.LAND)
ax.add_feature(cfeature.OCEAN)
ax.add_feature(cfeature.COASTLINE)
ax.add_feature(cfeature.BORDERS,linestyle=":")
for name in bird_names:
ix = birddata['bird_name'] == name
x, y = birddata.longitude[ix], birddata.latitude[ix]
ax.plot(x, y,".", transform = ccrs.Geodetic(), label = name)
plt.legend(loc="upper left")
plt.savefig("map.pdf")
I am using Spyder 5 and python 3.9. But I have an invalid syntax for the line ax = plt.axis(projection=ccrs.Mercator())
If I run the program, I receive this message in the console:
error in ax
I am unable to find help on this. Moreover, if you go to this link, you will see that they use it exactly as I do. So what am I missing?
Thank you i advance for your help.

TypeError: join() argument must be str or bytes, not 'TextIOWrapper

I have features and a target variable which I am wanting to generate a Decision Tree. However, the code is throwing an error. Since the 'out file' did not generate an error, I figured there wouldn't be an error for the 'Source.from_file' either, but there is one.
import os
from graphviz import Source
from sklearn.tree import export_graphviz
f = open("C:/Users/julia/Desktop/iris_tree.dot", 'w')
export_graphviz(
tree_clf,
out_file=f,
feature_names=sample2[0:2],
class_names=sample2[5],
rounded=True,
filled=True
)
Source.from_file(f)
As noted in the docs, from_file accepts a string path, not a file object:
filename – Filename for loading/saving the source.
Just pass the path in:
import os
from graphviz import Source
from sklearn.tree import export_graphviz
path = "C:/Users/julia/Desktop/iris_tree.dot"
f = open(path, 'w')
export_graphviz(
tree_clf,
out_file=f,
feature_names=sample2[0:2],
class_names=sample2[5],
rounded=True,
filled=True
)
Source.from_file(path)

Is there any alternative for pd.notna ( pandas 0.24.2). It is not working in pandas 0.20.1?

"Code was developed in pandas=0.24.2, and I need to make the code work in pandas=0.20.1. What is the alternative for pd.notna as it is not working in pandas version 0.20.1.
df.loc[pd.notna(df["column_name"])].query(....).drop(....)
I need an alternative to pd.notna to fit in this line of code to work in pandas=0.20.1
import os
import subprocess
import pandas as pd
import sys
from StringIO import StringIO
from io import StringIO
cmd = 'NSLOOKUP email.fullcontact.com'
df = pd.DataFrame()
a = subprocess.Popen(cmd, stdout=subprocess.PIPE)
b = StringIO(a.communicate()[0].decode('utf-8'))
df = pd.read_csv(b, sep=",")
column = list(df.columns)
name = list(df.iloc[1])[0].strip('Name:').strip()
name

Python- iterating over multiple files to read into a data frame

Here is the working code:
from urllib.request import urlretrieve
import requests
import xlrd
import pandas as pd
WORKING CODE
icd9_link = "https://www.cob.cms.hhs.gov/Section111/assets/section111/Section111ValidICD9-2017.xlsx"
icd9_map= pd.read_excel(icd9_link, sheet_name=0, header=0)
NOT WORKING CODE
Define function which will name ICD_9_map_ and use correct link
fx = icd"{0}"_map_= pd.read_excel(icd"{1}"_link, sheet_name=0, header=0)
#
y = [9,10]
for x in y:
fx.format(x, x)

Resources