Data frame error when trying to display head - python

I'm trying to display the head 10, but keep getting:
TypeError: 'DataFrame' object is not callable
import matplotlib.pyplot as plt
import matplotlib
import pandas as pd
import numpy as np
%matplotlib inline
Pop_By_County = pd.read_csv("TexasPopByCounty2021.csv")
display(Pop_By_County.head(10))

try using>>>
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
%matplotlib inline
Pop_By_County = pd.read_csv("TexasPopByCounty2021.csv")
df = pd.DataFrame(Pop_By_Country)
df.head(10)

Related

i am getting blank chart after setting plt.ylim

import pandas as pd
import tkinter
import matplotlib.pyplot as plt
import matplotlib
import seaborn as sn
data= pd.read_csv(r'C:\Users\AmitSatappa\Desktop\praticalone\netflix_titles.csv',encoding='cp1252')
matplotlib.use('TkAgg')
duplicate= data[data.duplicated()]
data.drop_duplicates(inplace=True)
new_data= data[data.duplicated()]
nullvalue = data.isnull()
plt.ylim(1,3000)
sn.barplot(nullvalue)
plt.show()
[before setting the plt.ylim i was getting the graph but after setting the limit i am geeting it as blank onlyenter image description here](https://i.stack.imgur.com/mPHxz.png)

AttributeError: 'str' object has no attribute 'strftime' error faced

I am facing this problem while coding;
import pandas as pd
import matplotlib from matplotlib import dates as d
import datetime as dt
import matplotlib.pyplot as plt
import numpy as np
data = pd.read_excel(r'F:\Downloads\Book1.xlsx', index_col=0)
data.replace('NoData', np.nan, inplace= True)
data['Time'] = data.index.map(lambda x: x.strftime("%H:%M"))
Error:
AttributeError: 'str' object has no attribute 'strftime'
You can use the parse_dates argument in your read_excel() call, and this means pandas should convert the specified column to datetime objects:
import pandas as pd
import matplotlib from matplotlib import dates as d
import datetime as dt
import matplotlib.pyplot as plt
import numpy as np
data = pd.read_excel(r'F:\Downloads\Book1.xlsx', index_col=0, parse_dates=[0])
data.replace('NoData', np.nan, inplace= True)
data['Time'] = data.index.map(lambda x: x.strftime("%H:%M"))
The documentation for read_excel is here.
I think you can use the pd.to_datetime() function to get datetime as follows:
import pandas as pd
import matplotlib from matplotlib import dates as d
import datetime as dt
import matplotlib.pyplot as plt
import numpy as np
data = pd.read_excel(r'F:\Downloads\Book1.xlsx', index_col=0)
data.replace('NoData', np.nan, inplace= True)
data['Time'] = pd.to_datetime(data.index, format="%H:%M")
You can check the pandas documentation for further information here.

ValueError: You must specify a period or x must be a pandas object with a DatetimeIndex with a freq not set to None

Hello and thank you in advance for your help!
I am getting ValueError: You must specify a period or x must be a pandas object with a DatetimeIndex with a freq not set to None when I try to do a time series decomposition that pulls from GitHub. I think I have a basic understanding of the error, but I do not get this error when I directly pull the data from the file on my computer, instead of pulling from GitHub. Why do I only get this error when I pull my data from GitHub? And how should I change my code so that I no longer get this error?
import pandas as pd
import numpy as np
%matplotlib inline
from statsmodels.tsa.seasonal import seasonal_decompose
topsoil = pd.read_csv('https://raw.githubusercontent.com/the-
datadudes/deepSoilTemperature/master/meanDickinson.csv',parse_dates=True)
topsoil = topsoil.dropna()
topsoil.head()
topsoil.plot();
result = seasonal_decompose(topsoil['Topsoil'],model='ad')
from pylab import rcParams
rcParams['figure.figsize'] = 12,5
result.plot();
Try this:
import pandas as pd
import numpy as np
%matplotlib inline
from statsmodels.tsa.seasonal import seasonal_decompose
topsoil = pd.read_csv('https://raw.githubusercontent.com/the-datadudes/deepSoilTemperature/master/meanDickinson.csv',parse_dates=True)
topsoil = topsoil.dropna()
topsoil.head()
topsoil.plot();
topsoil['Date'] = pd.to_datetime(topsoil['Date'])
topsoil = topsoil.set_index('Date').asfreq('D')
result = seasonal_decompose(topsoil, model='ad')
from pylab import rcParams
rcParams['figure.figsize'] = 12,5
result.plot();
Output:
try adding this
freq=12, extrapolate_trend=12
Full code would look like:
from pylab import rcParams
import statsmodels.api as sm
rcParams['figure.figsize'] = 12, 8
decomposition = sm.tsa.seasonal_decompose(data.Column, model='additive', freq=12, extrapolate_trend=12)
fig = decomposition.plot()
plt.show()

Why this Python pandas DataFrame code does not work?

My code:
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
income_vs_hardship = %sql SELECT per_capita_income_, hardship_index FROM chicago_socioeconomic_data;
plot = sns.jointplot(x='per_capita_income_',y='hardship_index', data=pd.DataFrame(income_vs_hardship))
Correct answer:
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
income_vs_hardship = %sql SELECT per_capita_income_, hardship_index FROM chicago_socioeconomic_data;
plot = sns.jointplot(x='per_capita_income_',y='hardship_index', data=income_vs_hardship.DataFrame())
The only difference:
data=pd.DataFrame(income_vs_hardship) vs. data=income_vs_hardship.DataFrame()
If DataFrame is a method belongs to pandas, why my code does not work.
The error shows 'unable to interpret the per_capita_income.'
DataFrame is a class of the pandas module, not a method that you can apply to a DataFrame instance.
income_vs_hardship.DataFrame() can't be interpreted by Python, as income_vs_hardship has no DataFrame method. Instead, pd.DataFrame(income_vs_hardship) creates a DtaFrame object.

pandas_datareader cant print date on x axis

In following plot date is not showing on x axis what do to show date time stamp
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import datetime
import pandas_datareader as web
start=datetime.datetime(2014,1,8)
end =datetime.datetime(2019,1,8)
Tesla=web.DataReader('TSLA',"iex",start,end)
Ford=web.DataReader('F',"iex",start,end)
Tesla["open"].plot(label="Tesla",title="opening Price",figsize=(16,8))
plt.legend()
plt.show()
Your dataframe index is created as dtype object or string, let's convert index to DatetimeIndex using pd.to_datetime:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import datetime
import pandas_datareader as web
start=datetime.datetime(2014,1,8)
end =datetime.datetime(2019,1,8)
Tesla=web.DataReader('TSLA',"iex",start,end)
Ford=web.DataReader('F',"iex",start,end)
#Change Telsa index to datetime dtype
Tesla.index = pd.to_datetime(Tesla.index)
#Let's do Fords too
Ford.index = pd.to_datetime(Ford.index)
Tesla["open"].plot(label="Tesla",title="opening Price",figsize=(16,8))
plt.legend()
plt.show()
Output:

Categories