Вот краткое руководство, чтобы вы смотрели. Я надеюсь, что это поможет.
Из ссылки 2 вы видите
Massachusetts 641 2,940,199 449.05
Rhode Island 159 466,598 443.30
New Jersey 729 3,235,290 367.99
Connecticut 399 1,252,936 350.56
New York 1,630 6,286,916 324.43
, скопируйте тексты выше, вставьте и сохраните данные в congregation.txt
.
Ссылка 1сломана. Однако, предполагая, что данные о населении выглядят следующим образом,
Massachusetts 3,141,270
Rhode Island 530,698
New Jersey 4,335,399
Connecticut 2,134,935
New York 10,366,556
аналогичным образом скопируйте приведенные выше тексты, вставьте и сохраните данные в population.txt
.
Затем вы можете выполнить что-то вроде этого
import pandas as pd
import matplotlib.pyplot as plt
con = pd.read_csv('congregation.txt', sep=r'[ \t]{2,}',header=None, index_col=False,engine='python')
pop = pd.read_csv('population.txt', sep=r'[ \t]{2,}',header=None, index_col=False,engine='python')
#note concat and not append
#con[0] is state, con[2] is congregation, pop[1] is population
#print(con.head()) and print(pop.head()) to visualize if you are still confused
df = pd.concat([con[[0,2]],pop[1]],axis=1)
df.columns = ['State', 'Congregation', 'Population']
#need to do some cleaning here to convert numbers with comma to an integer
df['Congregation'] = df['Congregation'].apply(lambda t: t.replace(',','')).astype(int)
df['Population'] = df['Population'].apply(lambda t: t.replace(',','')).astype(int)
df.set_index('State',inplace=True)
print(df.head())
#at this stage your df looks like this
# Congregation Population
#State
#Massachusetts 2940199 3141270
#Rhode Island 466598 530698
#New Jersey 3235290 4335399
#Connecticut 1252936 2134935
#New York 6286916 10366556
Вывод
Примечание: здесь я оставляю другие состояния ради демонстрации, в противном случае, если это только Нью-Джерси , гистограмма будет выглядеть пустой.
ax = df.plot.bar()
plt.show()
![enter image description here](https://i.stack.imgur.com/YsRNh.png)
Редактировать: я имел в виду «приверженец», а не «собрание». Я ошибся там.