I am not able to take (let say) top 10 categories of my feature in hue
as well as col
parameter using catplot
graph in seaborn.
def categorical_plot(data , feature1 , feature2 , col_feature ,hue_feature , plot_type):
plt.figure(figsize = (15,6))
ax = sns.catplot(feature1, feature2 , data =data, \
order = data[feature1].value_counts()[:10].index , \
col_wrap = 3, \
col = data.col_feature.value_counts()[:10].index, \
hue = data.hue_feature.value_counts()[:10].index, \
kind = plot_type, \
ci = False, \
palette = sns.color_palette("copper", 7),\
edgecolor=sns.color_palette('dark',7))
ax.set_xticklabels(rotation = 90)
plt.show()
function as called:
categorical_plot(dataframe, 'country_txt' , 'casualities' , 'nationality' , 'group' , 'bar' )
Using data.hue_feature.value_counts()[:10].index
doesn't work.
Data:
,year,month,day,extended,country_txt,region_txt,province,city,latitude,longitude,vicinity,crit1,crit2,crit3,doubtterr,multiple,success,suicide,attacktype,target_type,targsubtype1,target_subtype,target_entity,nationality,group,individual,weapon_type,weapon_subtype,killed,wounded,property,hostages/kidnapping,cross_border,ideological_international,Date,week,day_name,weekday,is_weekend,casualities
0,1970,7,2,0,Dominican Republic,Central America & Caribbean,,Santo Domingo,18.456792,-69.951164,0,1,1,1,0.0,0.0,1,0,Assassination,Private Citizens & Property,68.0,Named Civilian,Julio Guzman,Dominican Republic,MANO-D,0,Unknown,,1.0,0.0,0,0.0,0,0,1970-07-02,27,Thursday,3.0,0,1.0
1,1970,1,1,0,United States,North America,Illinois,Cairo,37.005105,-89.17626899999999,0,1,1,1,0.0,0.0,1,0,Armed Assault,Police,22.0,"Police Building (headquarters, station, school)",Cairo Police Headquarters,United States,Black Nationalists,0,Firearms,Unknown Gun Type,0.0,0.0,1,0.0,-9,-9,1970-01-01,1,Thursday,3.0,0,0.0
2,1970,1,2,0,Uruguay,South America,Montevideo,Montevideo,-34.891151,-56.187214000000004,0,1,1,1,0.0,0.0,0,0,Assassination,Police,25.0,Police Security Forces/Officers,Juan Maria de Lucah/Chief of Directorate of info. and intell.,Uruguay,Tupamaros (Uruguay),0,Firearms,Automatic or Semi-Automatic Rifle,0.0,0.0,0,0.0,0,0,1970-01-02,1,Friday,4.0,0,0.0
3,1970,1,2,0,United States,North America,California,Oakland,37.791927,-122.22590600000001,0,1,1,1,1.0,0.0,1,0,Bombing/Explosion,Utilities,107.0,Electricity,Edes Substation,United States,Unknown,0,Explosives,Unknown Explosive Type,0.0,0.0,1,0.0,-9,-9,1970-01-02,1,Friday,4.0,0,0.0
4,1970,1,2,0,United States,North America,Wisconsin,Madison,43.076592,-89.412488,0,1,1,1,0.0,0.0,1,0,Facility/Infrastructure Attack,Military,28.0,Military Recruiting Station/Academy,"R.O.T.C. offices at University of Wisconsin, Madison",United States,New Year's Gang,0,Incendiary,Molotov Cocktail/Petrol Bomb,0.0,0.0,1,0.0,0,0,1970-01-02,1,Friday,4.0,0,0.0
5,1970,1,3,0,United States,North America,Wisconsin,Madison,43.07295,-89.38669399999999,0,1,1,1,0.0,0.0,1,0,Facility/Infrastructure Attack,Government (General),21.0,Government Building/Facility/Office,Selective Service Headquarters in Madison Wisconsin,United States,New Year's Gang,0,Incendiary,Gasoline or Alcohol,0.0,0.0,1,0.0,0,0,1970-01-03,1,Saturday,5.0,1,0.0
6,1970,1,1,0,United States,North America,Wisconsin,Baraboo,43.4685,-89.744299,0,1,1,0,1.0,0.0,0,0,Bombing/Explosion,Military,27.0,Military Barracks/Base/Headquarters/Checkpost,Badger Army ammo depot.,United States,"Weather Underground, Weathermen",0,Explosives,Unknown Explosive Type,0.0,0.0,0,0.0,0,0,1970-01-01,1,Thursday,3.0,0,0.0
7,1970,1,6,0,United States,North America,Colorado,Denver,39.758967999999996,-104.876305,0,1,1,1,1.0,0.0,1,0,Facility/Infrastructure Attack,Military,28.0,Military Recruiting Station/Academy,"Army Recruiting Station, Denver Colorado",United States,Left-Wing Militants,0,Incendiary,Molotov Cocktail/Petrol Bomb,0.0,0.0,1,0.0,-9,-9,1970-01-06,2,Tuesday,1.0,0,0.0
8,1970,1,8,0,Italy,Western Europe,Lazio,Rome,41.890961,12.490069,0,1,1,1,-9.0,0.0,1,0,Hijacking,Airports & Aircraft,42.0,Aircraft (not at an airport),Flight 802 Boeing 707,United States,Unknown,0,Firearms,Rifle/Shotgun (non-automatic),0.0,0.0,0,0.0,-9,-9,1970-01-08,2,Thursday,3.0,0,0.0
9,1970,1,9,0,United States,North America,Michigan,Detroit,42.331685,-83.047924,0,1,1,1,0.0,0.0,1,0,Facility/Infrastructure Attack,Government (General),21.0,Government Building/Facility/Office,Packard Properties building of Detroit Michigan,United States,Left-Wing Militants,0,Incendiary,Molotov Cocktail/Petrol Bomb,0.0,0.0,1,0.0,-9,-9,1970-01-09,2,Friday,4.0,0,0.0
10,1970,1,9,0,United States,North America,Puerto Rico,Rio Piedras,18.386932,-66.061127,0,1,1,1,1.0,0.0,1,0,Facility/Infrastructure Attack,Business,7.0,Retail/Grocery/Bakery,Baker's Store,United States,Armed Commandos of Liberation,0,Incendiary,Arson/Fire,0.0,0.0,1,0.0,0,0,1970-01-09,2,Friday,4.0,0,0.0
11,1970,1,10,0,East Germany (GDR),Eastern Europe,Berlin,Berlin,52.50153,13.401851,0,1,1,1,0.0,0.0,1,0,Bombing/Explosion,Government (General),,,Jurists Ball (Palais au Frankturm),Germany,Commune 1,0,Explosives,Unknown Explosive Type,,,0,0.0,0,1,1970-01-10,2,Saturday,5.0,1,
12,1970,1,12,0,United States,North America,New York,New York City,40.697132,-73.931351,0,1,1,1,0.0,0.0,1,0,Bombing/Explosion,Educational Institution,49.0,School/University/Educational Building,James Madison High School,United States,Black Nationalists,0,Explosives,Pipe Bomb,0.0,0.0,1,0.0,-9,-9,1970-01-12,3,Monday,0.0,0,0.0
13,1970,1,12,0,United States,North America,Puerto Rico,Rio Grande,18.379998,-65.83094799999999,0,1,1,1,0.0,0.0,1,0,Bombing/Explosion,Business,4.0,Multinational Corporation,General Electric factory in Rio Grande Puerto Rico,United States,Strikers,0,Explosives,Unknown Explosive Type,0.0,0.0,-9,0.0,-9,-9,1970-01-12,3,Monday,0.0,0,0.0
14,1970,1,13,0,United States,North America,Washington,Seattle,47.610786,-122.33130600000001,0,1,1,1,0.0,0.0,1,0,Facility/Infrastructure Attack,Business,7.0,Retail/Grocery/Bakery,"Fuson's Department Store, Seattle Washington",United States,Black Nationalists,0,Incendiary,Molotov Cocktail/Petrol Bomb,0.0,0.0,1,0.0,-9,-9,1970-01-13,3,Tuesday,1.0,0,0.0
15,1970,1,14,0,United States,North America,Illinois,Champaign,40.116748,-88.23926999999999,0,1,1,1,0.0,0.0,1,0,Facility/Infrastructure Attack,Police,22.0,"Police Building (headquarters, station, school)",Champaign Police Department,United States,Black Nationalists,0,Incendiary,Molotov Cocktail/Petrol Bomb,0.0,1.0,1,0.0,-9,-9,1970-01-14,3,Wednesday,2.0,0,1.0
16,1970,1,15,0,Uruguay,South America,Montevideo,Montevideo,-34.891151,-56.187214000000004,0,1,1,1,0.0,0.0,1,0,Armed Assault,Educational Institution,49.0,School/University/Educational Building,"Secondary School, Chemistry Lab",Uruguay,Tupamaros (Uruguay),0,Firearms,Automatic or Semi-Automatic Rifle,0.0,0.0,1,0.0,0,0,1970-01-15,3,Thursday,3.0,0,0.0
17,1970,1,19,0,United States,North America,Washington,Seattle,47.610786,-122.33130600000001,0,1,1,1,0.0,0.0,1,0,Bombing/Explosion,Educational Institution,49.0,School/University/Educational Building,"Liberal Arts and Garrand buildings, Seattle University",United States,Black Nationalists,0,Explosives,Unknown Explosive Type,0.0,0.0,1,0.0,-9,-9,1970-01-19,4,Monday,0.0,0,0.0
18,1970,1,19,0,United States,North America,Washington,Seattle,47.610786,-122.33130600000001,0,1,1,1,0.0,0.0,0,0,Bombing/Explosion,Military,28.0,Military Recruiting Station/Academy,Air Force R.O.T.C. at the University of Washington,United States,Student Radicals,0,Explosives,Unknown Explosive Type,0.0,0.0,0,0.0,-9,-9,1970-01-19,4,Monday,0.0,0,0.0
19,1970,1,19,0,United States,North America,New Jersey,Jersey City,40.717892,-74.06746700000001,0,1,1,1,0.0,0.0,1,0,Facility/Infrastructure Attack,Violent Political Party,110.0,Party Office/Facility,Headquarters,United States,White extremists,0,Incendiary,Arson/Fire,0.0,0.0,1,0.0,-9,-9,1970-01-19,4,Monday,0.0,0,0.0
20,1970,1,20,0,Guatemala,Central America & Caribbean,Guatemala,Guatemala City,14.622869,-90.52906800000001,0,1,1,1,0.0,0.0,1,0,Assassination,Government (Diplomatic),46.0,Embassy/Consulate,"Bodyguard, British Consul",Great Britain,Unknown,0,Unknown,,1.0,0.0,0,0.0,-9,-9,1970-01-20,4,Tuesday,1.0,0,1.0
21,1970,1,21,0,Philippines,Southeast Asia,Metropolitan Manila,Quezon City,14.67428,121.05749499999999,0,1,1,1,0.0,0.0,1,0,Bombing/Explosion,Government (Diplomatic),46.0,Embassy/Consulate,JUSMAG HQ,United States,Unknown,0,Explosives,Vehicle,0.0,0.0,1,0.0,-9,-9,1970-01-21,4,Wednesday,2.0,0,0.0
22,1970,1,22,0,Venezuela,South America,Caracas,Caracas,10.482834,-66.962128,0,1,1,1,0.0,0.0,1,0,Hostage Taking (Kidnapping),Business,7.0,Retail/Grocery/Bakery,"Leon Jacobo Taurel, son",Venezuela,Armed Forces of National Liberation- Venezuela (FALN),0,Unknown,,0.0,0.0,0,1.0,0,0,1970-01-22,4,Thursday,3.0,0,0.0
23,1970,1,22,0,United States,North America,Nebraska,South Sioux City,42.47031,-96.413949,0,1,1,1,0.0,0.0,1,0,Bombing/Explosion,Private Citizens & Property,76.0,House/Apartment/Residence,,United States,Strikers,0,Explosives,Dynamite/TNT,0.0,0.0,1,0.0,-9,-9,1970-01-22,4,Thursday,3.0,0,0.0
24,1970,1,25,0,United States,North America,Mississippi,West Point,33.60651,-88.650419,0,1,1,1,1.0,0.0,1,0,Facility/Infrastructure Attack,Private Citizens & Property,71.0,Race/Ethnicity Identified,Building used as a meeting place for African Americans.,United States,White extremists,0,Incendiary,Arson/Fire,0.0,0.0,1,0.0,-9,-9,1970-01-25,4,Sunday,6.0,1,0.0
25,1970,1,25,0,United States,North America,New York,New York City,40.697132,-73.931351,0,1,1,1,0.0,0.0,1,0,Armed Assault,Police,25.0,Police Security Forces/Officers,New York Police Officers responding to a call in Harlem,United States,Black Nationalists,0,Firearms,Unknown Gun Type,0.0,2.0,1,0.0,-9,-9,1970-01-25,4,Sunday,6.0,1,2.0
26,1970,1,26,0,United States,North America,Mississippi,West Point,33.60651,-88.650419,0,1,1,1,0.0,0.0,1,0,Bombing/Explosion,Government (General),14.0,Judge/Attorney/Court,Clay County Courthouse,United States,Black Nationalists,0,Explosives,Dynamite/TNT,0.0,0.0,1,0.0,-9,-9,1970-01-26,5,Monday,0.0,0,0.0
27,1970,1,26,0,United States,North America,New York,New York City,40.697132,-73.931351,0,1,1,1,0.0,0.0,1,0,Armed Assault,Police,23.0,Police Patrol (including vehicles and convoys),New York Police Officer on patrol in Forest Hills,United States,Black Nationalists,0,Firearms,Unknown Gun Type,0.0,0.0,0,0.0,-9,-9,1970-01-26,5,Monday,0.0,0,0.0
28,1970,1,27,0,United States,North America,Ohio,Norwalk,41.241996,-82.615241,0,1,1,1,0.0,0.0,1,0,Facility/Infrastructure Attack,Government (General),21.0,Government Building/Facility/Office,Selective Service office in Norwalk Ohio,United States,Left-Wing Militants,0,Incendiary,Arson/Fire,0.0,0.0,1,0.0,-9,-9,1970-01-27,5,Tuesday,1.0,0,0.0
29,1970,1,28,0,East Germany (GDR),Eastern Europe,Berlin,Berlin,52.50153,13.401851,0,1,1,1,-9.0,0.0,1,0,Facility/Infrastructure Attack,Police,25.0,Police Security Forces/Officers,Residence of President of Penal Office,Germany,Unknown,0,Incendiary,Molotov Cocktail/Petrol Bomb,,,1,0.0,-9,-9,1970-01-28,5,Wednesday,2.0,0,
Is there any way to solve this?
seaborn.catplot
col_feature
andhue_feature
are strings, and a string can't be used for.
notation when accessing a dataframe column.data.col_feature
is equivalent todata.'nationality'
and won't workdata[col_feature]
which is equivalent todata['nationality']
col
parameter expects a column name,col='nationality'
, not an array of values from inside the column.data[col_feature].value_counts()[:10].index
can't be usedhue
parameter also expects a column name,'hue='group'
, not an array.data[hue_feature].value_counts()[:10].index
can't be usedcatplot
.