pd.get_dummies syntax error python - python-3.x

I am getting an error while using pandas get_dummies command. Can someone point out why?
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
plt.style.use('ggplot')
s = pd.read_csv('kddcup.txt')
t=s.apply(pd.Series.nunique)
print(t.index)
r = s.columns
print(r)
for n in range (0,len(t.index)):
if t[r[n]] == 1:
del s[r[n]
s = pd.get_dummies(s)//getting syntax error error here.
Spyder error code

You need to change del s[r[n] to del s[r[n]]. Each opening bracket must have a closing one.

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.

python pandas dataframe.plot()

Hello I am working on assignment and I am having trouble. When I run the program it compiles and there are no errors. however, nothing is printed out on the terminal screen. I guess I expect a graph on the terminal screen but don't see anything
with open('myfile.csv') as csvfile:
data = pd.read_csv(csvfile, delimiter=',')
d = data.values
dd = pd.DataFrame(data=d)
dd.plot()
Any tips or suggestions is appreciated
import pandas as pd
import matplotlib.pyplot as plt
with open('myfile.csv') as csvfile:
data = pd.read_csv(csvfile, delimiter=',')
d = data.values
dd = pd.DataFrame(data=d)
dd.plot()
plt.show()
use this
The graph is plotting with your current code as is,
Please check the imports once again. I'm using the following imports:
import pandas as pd
pd.plotting.register_matplotlib_converters()
import matplotlib.pyplot as plt
%matplotlib inline

Pandas error "No numeric data to plot" when using stock data from datareader

I have a dataframe with closing stock prices:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn; seaborn.set()
from pandas_datareader import data
import pandas_datareader.data as web
from pandas.tseries.offsets import BDay
f = web.DataReader('^DJI', 'stooq')
CLOSE = f['Close']
CLOSE.plot(alpha= 0.5,style='-')
CLOSE.resample('BA').mean().plot(style=':')
CLOSE.asfreq(freq='BA').plot(style='--')
plt.legend(['input','resample','asfreq'],loc='upper left')
With resample() I get the average of the previous year. This works.
With asfreq() I try to get the closing value at the end of the year. This doesn't work.
I get the following error in the asfreq() line: TypeError: no numeric data to plot
f.info() displays that close is a non-null float64 type.
What could be wrong?
The indices were not hierachically sorted:
f= f.sort_index(axis=0) solved it.

TypeError: 'function' object is not subscriptable. This is my error

I am trying to create a Time-Series model for forecasting few values. But whenever I am trying to read the file, I am getting an error.
The line which is in bold and italic is generating error.
This is my code :
import warnings
import itertools
import numpy as np
import matplotlib.pyplot as plt
warnings.filterwarnings("ignore")
plt.style.use('fivethirtyeight')
import pandas as pd
import statsmodels.api as sm
import matplotlib
matplotlib.rcParams['axes.labelsize'] = 14
matplotlib.rcParams['xtick.labelsize'] = 12
matplotlib.rcParams['ytick.labelsize'] = 12
matplotlib.rcParams['text.color'] = 'k'
***df = pd.read_csv["forecasting.csv", ","]***
t1 = df.loc[df['Technology'] == 't_1']
Use round brackets - ( and ), not square brackets [ and ], i.e.
df = pd.read_csv("forecasting.csv", ",")

Resources