I cannot extract data from a specific column

Asked

Viewed 74 times

0

My Dataframe has several columns and one of them is not being read when I try to use indexing (e.g., bar_0617 = media_0617['Bar']). The following error appears: Keyerror: 'Bar'. I do not know what to do, because I have already checked the file with my data and the column has this name, when calculating the average also appears the correct name, but when I index to go through your data gives error. Thanks in advance" Follow the code

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

file = r'C:\Users\Jonat\OneDrive\Documentos\Faculdade\LAMCE\dadostratados1718.csv'
table = pd.read_csv(file, sep = ';')

#2017
table_2017 = table.query('ANO==2017')

#Cada mês
table_0617 = table_2017.query('MES==6')
table_0717 = table_2017.query('MES==7')
table_0817 = table_2017.query('MES==8')
table_0917 = table_2017.query('MES==9')
table_1017 = table_2017.query('MES==10')
table_1117 = table_2017.query('MES==11')
table_1217 = table_2017.query('MES==12')

media_0617 = np.mean(table_0617)
media_0717 = np.mean(table_0717)
media_0817 = np.mean(table_0817)
media_0917 = np.mean(table_0917)
media_1017 = np.mean(table_1017)
media_1117 = np.mean(table_1117)
media_1217 = np.mean(table_1217)

time = ['0617', '0717', '0817', '0917', '1017', '1117', '1217']
time = pd.DataFrame({'Tempo': time})

bar_0617 = media_0617['Bar']
bar_0717 = media_0717['Bar']
bar_0817 = media_0817['Bar']
bar_0917 = media_0917['Bar']
bar_1017 = media_1017['Bar']
bar_1117 = media_1117['Bar']
bar_1217 = media_1217['Bar']

bar = [bar_0617, bar_0717, bar_0817, bar_0917, bar_1017, bar_1117, bar_1217, bar_0118, bar_0218, bar_0318, bar_0418, bar_0518, bar_0618, bar_0718, bar_0818, bar_0918, bar_1018, bar_1118, bar_1218]
bar = pd.DataFrame({'Bar': bar})

serie_bar = pd.concat([bar, time], axis=1, join='inner')   #Pressão

plt.plot(serie_bar['Tempo'], serie_bar['Bar'])        #Pressão
plt.title('Pressão x Tempo', fontsize = '20')
plt.ylabel('Pressão (hPa)')
plt.xlabel('tempo')
plt.rcParams['figure.figsize'] = (11,7)
plt.savefig(r"C:\Users\Jonat\OneDrive\Documentos\Faculdade\LAMCE\variáveis\serie_bar.png", dpi=None, facecolor='w', edgecolor='w',orientation='landscape')
plt.show()

1 answer

0

He simply did not identify the Bar column in the media variables... Are you sure this column exists? It is possible to make this dataset available or use a dataset.info() to see how it is composed?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.