Demo entry 6782474

exe10

   

Submitted by anonymous on Jan 15, 2019 at 20:34
Language: Python. Code size: 127.6 kB.

028806446 - exe10 

import pandas as pd 
# Read data from file 'file_name.csv'


file_name = '/Users\Rony\Desktop\PYTHON_EXamples folder\fortune1000.csv'
# (in the same directory that your python process is based)
# Control delimiters, rows, column names with read_csv (see later) 
pd.read_csv(file_name = "file_name.csv") 
# Preview the first 5 lines of the loaded data 
pd.columns("file_name.csv")

"""

from pandas import DataFrame, read_csv
import matplotlib.pyplot as plt
import pandas as pd 
 
file = 'c:\\Users\\Rony\\Desktop\\PYTHON EXamples folder\\fortune1000.csv'
df = pd.read_csv(file)
print(df)
df.head()
##a
y = df.index
len(y)  ###1000
#check 
w = df.tail
print(w)
z = df.columns
len(z)    ####8 columns 
###1000 rows X 8 columns

#b. mean revenue and mean profits
df.Revenue
df.Profits
revenue = df.Revenue
revenue.mean()   # 13535.525
profits = df.Profits
profits.mean()   #894.093
profits.describe()   
"""
>>> profits.describe()
count     1000.000000
mean       894.093000
std       3297.063594
min     -23119.000000
25%         82.750000
50%        282.500000
75%        784.500000
max      53394.000000
""""

##c. 7 successful companies with highest revenues
from pandas import DataFrame, read_csv
import matplotlib.pyplot as plt
import pandas as pd 
 
file = 'c:\\Users\\Rony\\Desktop\\PYTHON EXamples folder\\fortune1000.csv'
df = pd.read_csv(file)

sorting = df.sort_values(by=['Profits']) 
sorting.tail(7)
"""
>>> sorting.tail(7)
    Rank             Company              Sector                                  Industry           Location  Revenue  Profits  Employees
28    29           Citigroup          Financials                          Commercial Banks       New York, NY    88275    17242     231000
12    13             Verizon  Telecommunications                        Telecommunications       New York, NY   131620    17879     177700
85    86     Gilead Sciences         Health Care                           Pharmaceuticals    Foster City, CA    32639    18108       8000
26    27         Wells Fargo          Financials                          Commercial Banks  San Francisco, CA    90033    22894     264700
3      4  Berkshire Hathaway          Financials  Insurance: Property and Casualty (Stock)          Omaha, NE   210821    24083     331000
22    23   J.P. Morgan Chase          Financials                          Commercial Banks       New York, NY   101006    24442     234598
2      3               Apple          Technology               Computers, Office Equipment      Cupertino, CA   233715    53394     110000
>>>
"""
##d. finding the firms with number of employees higher than 300,000 

df1 = df
df1.loc[df1.Employees<300000]='NA'
#print(df1)
 #filter out rows ina . dataframe with column Employees values NA/NAN
df1_not_NA = df1[df1.Employees.notnull()]
print(df1_not_NA)    #####לא מצליחה לסנן בהדפסה

     
###e . number of sectors and number of industries 
###for that we need to groupby sector and industries first 

df.groupby(['Sector']).agg(sum)

"""
>>> df.groupby(['Sector']).agg(sum)
                                                                           Rank                        ...                                                                  Employees
Sector                                                                                                 ...
Financials                                                                    4                        ...                                                                     331000
Food and Drug Stores                                                         36                        ...                                                                     733500
Hotels, Resturants & Leisure                                                327                        ...                                                                     925000
Industrials                                                                  11                        ...                                                                     333000
N/A                           N/AN/AN/AN/AN/AN/AN/AN/AN/AN/AN/AN/AN/AN/AN/AN...                        ...                          N/AN/AN/AN/AN/AN/AN/AN/AN/AN/AN/AN/AN/AN/AN/AN...
Retailing                                                                    67                        ...                                                                    3026000
Technology                                                                   31                        ...                                                                     411798
Transportation                                                              106                        ...                                                                     664275

[8 rows x 7 columns]
""""

df.groupby(['Industry']).agg(sum)

"""
>>> df.groupby(['Industry']).agg(sum)
                                                                                       Rank                        ...                                                                  Employees
Industry                                                                                                           ...
Food Services                                                                           327                        ...                                                                     925000
Food and Drug Stores                                                                     36                        ...                                                                     733500
General Merchandisers                                                                    39                        ...                                                                    2641000
Industrial Machinery                                                                     11                        ...                                                                     333000
Information Technology Services                                                          31                        ...                                                                     411798
Insurance: Property and Casualty (Stock)                                                  4                        ...                                                                     331000
Mail, Package, and Freight Delivery                                                     106                        ...                                                                     664275
N/A                                       N/AN/AN/AN/AN/AN/AN/AN/AN/AN/AN/AN/AN/AN/AN/AN...                        ...                          N/AN/AN/AN/AN/AN/AN/AN/AN/AN/AN/AN/AN/AN/AN/AN...
Specialty Retailers: Other                                                               28                        ...                                                                     385000

[9 rows x 7 columns]
""""

#results are 7 sectors and 8 industries
#
# #f. how many companies each industry has and what are 5 most popular industries  
from pandas import DataFrame, read_csv
import matplotlib.pyplot as plt
import pandas as pd 


file = 'c:\\Users\\Rony\\Desktop\\PYTHON EXamples folder\\fortune1000.csv'
df = pd.read_csv(file)
 
df2 = df.groupby(['Industry','Company'])['Location'].agg('sum')
df2.count()

df.groupby(['Industry','Company','Location'])['Rank'].agg(['sum','count'])

df.groupby(['Industry','Company'])['Location'].count().reset_index()
df.groupby(['Industry','Company']).agg('count').reset_index()


#g. splitting the location onto two columns 

from pandas import DataFrame, read_csv
import matplotlib.pyplot as plt
import pandas as pd 


file = 'c:\\Users\\Rony\\Desktop\\PYTHON EXamples folder\\fortune1000.csv'
df = pd.read_csv(file)


fortune1000['City'],fortune1000['State'] =fortune1000.Location.str.split(',').str

print(df)


""""
example to partial output

9>> print(df)
     Rank                            Company                        Sector                                    Industry           Location  Revenue  Profits  Employees           City State
0       1                            Walmart                     Retailing                       General Merchandisers    Bentonville, AR   482130    14694    2300000    Bentonville    AR
1       2                        Exxon Mobil                        Energy                          Petroleum Refining         Irving, TX   246204    16150      75600         Irving    TX
2       3                              Apple                    Technology                 Computers, Office Equipment      Cupertino, CA   233715    53394     110000      Cupertino    CA
3       4                 Berkshire Hathaway                    Financials    Insurance: Property and Casualty (Stock)          Omaha, NE   210821    24083     331000          Omaha    NE
4       5                           McKesson                   Health Care                    Wholesalers: Health Care  San Francisco, CA   181241     1476      70400  San Francisco    CA
5       6                 UnitedHealth Group                   Health Care     Health Care: Insurance and Managed Care     Minnetonka, MN   157107     5813     200000     Minnetonka    MN
6       7                         CVS Health          Food and Drug Stores                        Food and Drug Stores     Woonsocket, RI   153290     5237     199000     Woonsocket    RI
7       8                     General Motors        Motor Vehicles & Parts                    Motor Vehicles and Parts        Detroit, MI   152356     9687     215000        Detroit    MI
8       9                         Ford Motor        Motor Vehicles & Parts                    Motor Vehicles and Parts       Dearborn, MI   149558     7373     199000       Dearborn    MI
9      10                               AT&T            Telecommunications                          Telecommunications         Dallas, TX   146801    13345     281450         Dallas    TX
10     11                   General Electric                   Industrials                        Industrial Machinery      Fairfield, CT   140389    -6126     333000      Fairfield    CT
11     12                  AmerisourceBergen                   Health Care                    Wholesalers: Health Care   Chesterbrook, PA   135962     -135      17000   Chesterbrook    PA
12     13                            Verizon            Telecommunications                          Telecommunications       New York, NY   131620    17879     177700       New York    NY
13     14                            Chevron                        Energy                          Petroleum Refining      San Ramon, CA   131118     4587      61500      San Ramon    CA
14     15                             Costco                     Retailing                  Specialty Retailers: Other       Issaquah, WA   116199     2377     161000       Issaquah    WA
15     16                         Fannie Mae                    Financials                      Diversified Financials     Washington, DC   110359    10954       7300     Washington    DC
16     17                             Kroger          Food and Drug Stores                        Food and Drug Stores     Cincinnati, OH   109830     2039     431000     Cincinnati    OH
17     18                         Amazon.com                    Technology             Internet Services and Retailing        Seattle, WA   107006      596     230800        Seattle    WA
18     19           Walgreens Boots Alliance          Food and Drug Stores                        Food and Drug Stores      Deerfield, IL   103444     4220     302500      Deerfield    IL
19     20                                 HP                    Technology                 Computers, Office Equipment      Palo Alto, CA   103355     4554     287000      Palo Alto    CA
20     21                    Cardinal Health                   Health Care                    Wholesalers: Health Care         Dublin, OH   102531     1215      34500         Dublin    OH
21     22            Express Scripts Holding                   Health Care    Health Care: Pharmacy and Other Services      St. Louis, MO   101752     2476      25900      St. Louis    MO
22     23                  J.P. Morgan Chase                    Financials                            Commercial Banks       New York, NY   101006    24442     234598       New York    NY
23     24                             Boeing           Aerospace & Defense                       Aerospace and Defense        Chicago, IL    96114     5176     161400        Chicago    IL
24     25                          Microsoft                    Technology                           Computer Software        Redmond, WA    93580    12193     118000        Redmond    WA
25     26              Bank of America Corp.                    Financials                            Commercial Banks      Charlotte, NC    93056    15888     213280      Charlotte    NC
26     27                        Wells Fargo                    Financials                            Commercial Banks  San Francisco, CA    90033    22894     264700  San Francisco    CA
27     28                         Home Depot                     Retailing                  Specialty Retailers: Other        Atlanta, GA    88519     7009     385000        Atlanta    GA
28     29                          Citigroup                    Financials                            Commercial Banks       New York, NY    88275    17242     231000       New York    NY
29     30                        Phillips 66                        Energy                          Petroleum Refining        Houston, TX    87169     4227      14000        Houston    TX
..    ...                                ...                           ...                                         ...                ...      ...      ...        ...            ...   ...
970   971                   VeriFone Systems                    Technology                 Computers, Office Equipment       San Jose, CA     2001       79       5400       San Jose    CA
971   972                  Genesee & Wyoming                Transportation                                   Railroads         Darien, CT     2000      225       7500         Darien    CT
972   973               99 Cents Only Stores                     Retailing                  Specialty Retailers: Other       Commerce, CA     1999     -232      18200       Commerce    CA
973   974  Roadrunner Transportation Systems                Transportation                Transportation and Logistics         Cudahy, WI     1995       48       4502         Cudahy    WI
974   975               Super Micro Computer                    Technology                 Computers, Office Equipment       San Jose, CA     1991      102       2285       San Jose    CA
975   976                First Republic Bank                    Financials                            Commercial Banks  San Francisco, CA     1989      522       3130  San Francisco    CA
976   977                  Hill-Rom Holdings                   Health Care              Medical Products and Equipment        Chicago, IL     1988       48      10000        Chicago    IL
977   978                 Providence Service                   Health Care    Health Care: Pharmacy and Other Services         Tucson, AZ     1987       84       9072         Tucson    AZ
978   979      Allison Transmission Holdings        Motor Vehicles & Parts                    Motor Vehicles and Parts   Indianaoplis, IN     1986      182       2700   Indianaoplis    IN
979   980                              Spire                        Energy                 Utilities: Gas and Electric      St. Louis, MO     1976      137       3078      St. Louis    MO
980   981                         WPX Energy                        Energy                Mining, Crude-Oil Production          Tulsa, OK     1958    -1727       1040          Tulsa    OK
981   982                   Century Aluminum                     Materials                                      Metals        Chicago, IL     1950      -59       1778        Chicago    IL
982   983           Adams Resources & Energy                        Energy                          Petroleum Refining        Houston, TX     1944       -1        809        Houston    TX
983   984              Nuance Communications                    Technology                           Computer Software     Burlington, MA     1931     -115      13500     Burlington    MA
984   985                  Primoris Services    Engineering & Construction                   Engineering, Construction         Dallas, TX     1929       37       7011         Dallas    TX
985   986         Schnitzer Steel Industries                     Materials                                      Metals       Portland, OR     1924     -197       2955       Portland    OR
986   987              Delta Tucker Holdings           Aerospace & Defense                       Aerospace and Defense         McLean, VA     1923     -133      12000         McLean    VA
987   988       Hospitality Properties Trust                    Financials                                 Real estate         Newton, MA     1922      166        400         Newton    MA
988   989                             Cenveo                         Media                        Publishing, Printing       Stamford, CT     1921      -31       7300       Stamford    CT
989   990                        F5 Networks                    Technology  Network and Other Communications Equipment        Seattle, WA     1920      365       4178        Seattle    WA
990   991                  BlueLinx Holdings                   Wholesalers                    Wholesalers: Diversified        Atlanta, GA     1917      -12       1600        Atlanta    GA
991   992                             Revlon            Household Products             Household and Personal Products       New York, NY     1914       56       5700       New York    NY
992   993              DeVry Education Group             Business Services                                   Education  Downers Grove, IL     1910      140      11770  Downers Grove    IL
993   994                       MDC Holdings    Engineering & Construction                                Homebuilders         Denver, CO     1909       66       1225         Denver    CO
994   995                          EP Energy                        Energy                Mining, Crude-Oil Production        Houston, TX     1908    -3748        665        Houston    TX
995   996         New York Community Bancorp                    Financials                            Commercial Banks       Westbury, NY     1902      -47       3448       Westbury    NY
996   997          Portland General Electric                        Energy                 Utilities: Gas and Electric       Portland, OR     1898      172       2646       Portland    OR
997   997          Portland General Electric                        Energy                 Utilities: Gas and Electric       Portland, OR     1898      172       2646       Portland    OR
998   999                            Wendys  Hotels, Resturants & Leisure                               Food Services         Dublin, OH     1896      161      21200         Dublin    OH
999  1000                  Briggs & Stratton                   Industrials                        Industrial Machinery      Wauwatosa, WI     1895       46       5480      Wauwatosa    WI

[1000 rows x 10 columns]
>>>
>>>
""""
#### how many states represented and which is the most common
y = df.State
y.describe()
"""
>>> y.describe()
count     1000
unique      45
top         CA
freq       108
Name: State, dtype: object
there are just 45 states represented and CA is the freq. 
"""

###h. companies located in NY rank smaller than 50 
from pandas import DataFrame, read_csv
import matplotlib.pyplot as plt
import pandas as pd 


file = 'c:\\Users\\Rony\\Desktop\\PYTHON EXamples folder\\fortune1000.csv'
df = pd.read_csv(file)
fortune1000['City'],fortune1000['State'] =fortune1000.Location.str.split(',').str


df.loc[df['Rank'] < 50, 'State'] = 'NY'
print(df)

""""
question h 
df.loc[df['Rank'] < 50, 'State'] = 'NY'
print(df)
>>> print(df)
     Rank                            Company                        Sector                                    Industry           Location  Revenue  Profits  Employees State
0       1                            Walmart                     Retailing                       General Merchandisers    Bentonville, AR   482130    14694    2300000    NY
1       2                        Exxon Mobil                        Energy                          Petroleum Refining         Irving, TX   246204    16150      75600    NY
2       3                              Apple                    Technology                 Computers, Office Equipment      Cupertino, CA   233715    53394     110000    NY
3       4                 Berkshire Hathaway                    Financials    Insurance: Property and Casualty (Stock)          Omaha, NE   210821    24083     331000    NY
4       5                           McKesson                   Health Care                    Wholesalers: Health Care  San Francisco, CA   181241     1476      70400    NY
5       6                 UnitedHealth Group                   Health Care     Health Care: Insurance and Managed Care     Minnetonka, MN   157107     5813     200000    NY
6       7                         CVS Health          Food and Drug Stores                        Food and Drug Stores     Woonsocket, RI   153290     5237     199000    NY
7       8                     General Motors        Motor Vehicles & Parts                    Motor Vehicles and Parts        Detroit, MI   152356     9687     215000    NY
8       9                         Ford Motor        Motor Vehicles & Parts                    Motor Vehicles and Parts       Dearborn, MI   149558     7373     199000    NY
9      10                               AT&T            Telecommunications                          Telecommunications         Dallas, TX   146801    13345     281450    NY
10     11                   General Electric                   Industrials                        Industrial Machinery      Fairfield, CT   140389    -6126     333000    NY
11     12                  AmerisourceBergen                   Health Care                    Wholesalers: Health Care   Chesterbrook, PA   135962     -135      17000    NY
12     13                            Verizon            Telecommunications                          Telecommunications       New York, NY   131620    17879     177700    NY
13     14                            Chevron                        Energy                          Petroleum Refining      San Ramon, CA   131118     4587      61500    NY
14     15                             Costco                     Retailing                  Specialty Retailers: Other       Issaquah, WA   116199     2377     161000    NY
15     16                         Fannie Mae                    Financials                      Diversified Financials     Washington, DC   110359    10954       7300    NY
16     17                             Kroger          Food and Drug Stores                        Food and Drug Stores     Cincinnati, OH   109830     2039     431000    NY
17     18                         Amazon.com                    Technology             Internet Services and Retailing        Seattle, WA   107006      596     230800    NY
18     19           Walgreens Boots Alliance          Food and Drug Stores                        Food and Drug Stores      Deerfield, IL   103444     4220     302500    NY
19     20                                 HP                    Technology                 Computers, Office Equipment      Palo Alto, CA   103355     4554     287000    NY
20     21                    Cardinal Health                   Health Care                    Wholesalers: Health Care         Dublin, OH   102531     1215      34500    NY
21     22            Express Scripts Holding                   Health Care    Health Care: Pharmacy and Other Services      St. Louis, MO   101752     2476      25900    NY
22     23                  J.P. Morgan Chase                    Financials                            Commercial Banks       New York, NY   101006    24442     234598    NY
23     24                             Boeing           Aerospace & Defense                       Aerospace and Defense        Chicago, IL    96114     5176     161400    NY
24     25                          Microsoft                    Technology                           Computer Software        Redmond, WA    93580    12193     118000    NY
25     26              Bank of America Corp.                    Financials                            Commercial Banks      Charlotte, NC    93056    15888     213280    NY
26     27                        Wells Fargo                    Financials                            Commercial Banks  San Francisco, CA    90033    22894     264700    NY
27     28                         Home Depot                     Retailing                  Specialty Retailers: Other        Atlanta, GA    88519     7009     385000    NY
28     29                          Citigroup                    Financials                            Commercial Banks       New York, NY    88275    17242     231000    NY
29     30                        Phillips 66                        Energy                          Petroleum Refining        Houston, TX    87169     4227      14000    NY
..    ...        
""""

#question i - sectors assessment 
file = 'c:\\Users\\Rony\\Desktop\\PYTHON EXamples folder\\fortune1000.csv'
df = pd.read_csv(file)

#i built a function of means to learn on the average
df.groupby('Sector').mean()

""""
I have this table now :
>>> df.groupby('Sector').mean()
                                    Rank       Revenue      Profits     Employees
Sector
Aerospace & Defense           443.500000  17897.000000  1437.100000  48402.850000
Apparel                       583.800000   6397.866667   549.066667  23093.133333
Business Services             609.294118   5337.156863   553.470588  26687.254902
Chemicals                     530.933333   8129.900000   754.266667  15455.033333
Energy                        509.827869  12441.057377  -602.024590   9745.303279
Engineering & Construction    582.576923   5922.423077   204.000000  15642.615385
Financials                    457.309353  15950.784173  1872.007194  24172.287770
Food and Drug Stores          428.600000  32251.266667  1117.266667  93026.533333
Food, Beverages & Tobacco     433.232558  12929.465116  1195.744186  28177.488372
Health Care                   423.600000  21529.426667  1414.853333  35710.520000
Hotels, Resturants & Leisure  561.560000   6781.840000   827.880000  99369.800000
Household Products            562.464286   8383.464286   515.285714  23072.785714
Industrials                   586.695652  10816.978261   451.391304  33591.934783
Materials                     542.860465   6026.627907   102.976744  14840.069767
Media                         559.040000   8830.560000   973.880000  22012.560000
Motor Vehicles & Parts        475.333333  20105.833333  1079.083333  45106.666667
Retailing                     445.637500  18313.450000   597.875000  77845.362500
Technology                    557.862745  13505.882353  1769.343137  35087.735294
Telecommunications            325.133333  30788.933333  3242.466667  55497.866667
Transportation                509.694444  11347.444444  1226.916667  42688.694444
Wholesalers                   421.125000  11120.000000   205.825000  13139.925000
>>>
""""
#First I am checking the function of the profits
y = df.groupby('Sector').mean()
y.Profits.describe()
"""
>>> y.Profits.describe()
count      21.000000
mean      928.032108
std       792.832162
min      -602.024590   i will use that to find the industry energy
25%       515.285714
50%       827.880000
75%      1226.916667
max      3242.466667     i will use that to find the industry  telecomunication
Name: Profits, dtype: float64
>>>
""""


y.loc[['Profits']]['Sector'] == 3242.466667
print(y)


results : 

978   979      Allison Transmission Holdings        Motor Vehicles & Parts                    Motor Vehicles and Parts   Indianaoplis, IN     1986      182       2700
979   980                              Spire                        Energy                 Utilities: Gas and Electric      St. Louis, MO     1976      137       3078
980   981                         WPX Energy                        Energy                Mining, Crude-Oil Production          Tulsa, OK     1958    -1727       1040
981   982                   Century Aluminum                     Materials                                      Metals        Chicago, IL     1950      -59       1778
982   983           Adams Resources & Energy                        Energy                          Petroleum Refining        Houston, TX     1944       -1        809
983   984              Nuance Communications                    Technology                           Computer Software     Burlington, MA     1931     -115      13500
984   985                  Primoris Services    Engineering & Construction                   Engineering, Construction         Dallas, TX     1929       37       7011
985   986         Schnitzer Steel Industries                     Materials                                      Metals       Portland, OR     1924     -197       2955
986   987              Delta Tucker Holdings           Aerospace & Defense                       Aerospace and Defense         McLean, VA     1923     -133      12000
987   988       Hospitality Properties Trust                    Financials                                 Real estate         Newton, MA     1922      166        400
988   989                             Cenveo                         Media                        Publishing, Printing       Stamford, CT     1921      -31       7300
989   990                        F5 Networks                    Technology  Network and Other Communications Equipment        Seattle, WA     1920      365       4178
990   991                  BlueLinx Holdings                   Wholesalers                    Wholesalers: Diversified        Atlanta, GA     1917      -12       1600
991   992                             Revlon            Household Products             Household and Personal Products       New York, NY     1914       56       5700
992   993              DeVry Education Group             Business Services                                   Education  Downers Grove, IL     1910      140      11770
993   994                       MDC Holdings    Engineering & Construction                                Homebuilders         Denver, CO     1909       66       1225
994   995                          EP Energy                        Energy                Mining, Crude-Oil Production        Houston, TX     1908    -3748        665
995   996         New York Community Bancorp                    Financials                            Commercial Banks       Westbury, NY     1902      -47       3448
996   997          Portland General Electric                        Energy                 Utilities: Gas and Electric       Portland, OR     1898      172       2646
997   997          Portland General Electric                        Energy                 Utilities: Gas and Electric       Portland, OR     1898      172       2646
998   999                            Wendy’s  Hotels, Resturants & Leisure                               Food Services         Dublin, OH     1896      161      21200
999  1000                  Briggs & Stratton                   Industrials                        Industrial Machinery      Wauwatosa, WI     1895       46       5480

[1000 rows x 8 columns]>
>>> z = df.columns
>>> len(z)    ####8 columns
8
>>> ###1000 rows X 8 columns
...
>>> #b. mean revenue and mean profits
... df.Revenue
0      482130
1      246204
2      233715
3      210821
4      181241
5      157107
6      153290
7      152356
8      149558
9      146801
10     140389
11     135962
12     131620
13     131118
14     116199
15     110359
16     109830
17     107006
18     103444
19     103355
20     102531
21     101752
22     101006
23      96114
24      93580
25      93056
26      90033
27      88519
28      88275
29      87169
        ...
970      2001
971      2000
972      1999
973      1995
974      1991
975      1989
976      1988
977      1987
978      1986
979      1976
980      1958
981      1950
982      1944
983      1931
984      1929
985      1924
986      1923
987      1922
988      1921
989      1920
990      1917
991      1914
992      1910
993      1909
994      1908
995      1902
996      1898
997      1898
998      1896
999      1895
Name: Revenue, Length: 1000, dtype: int64
>>> df.Profits
0      14694
1      16150
2      53394
3      24083
4       1476
5       5813
6       5237
7       9687
8       7373
9      13345
10     -6126
11      -135
12     17879
13      4587
14      2377
15     10954
16      2039
17       596
18      4220
19      4554
20      1215
21      2476
22     24442
23      5176
24     12193
25     15888
26     22894
27      7009
28     17242
29      4227
       ...
970       79
971      225
972     -232
973       48
974      102
975      522
976       48
977       84
978      182
979      137
980    -1727
981      -59
982       -1
983     -115
984       37
985     -197
986     -133
987      166
988      -31
989      365
990      -12
991       56
992      140
993       66
994    -3748
995      -47
996      172
997      172
998      161
999       46
Name: Profits, Length: 1000, dtype: int64
>>> revenue = df.Revenue
>>> revenue.mean()   # 13535.525
13535.525
>>> profits = df.Profits
>>> profits.mean()   #894.093
894.093
>>> profits.describe()
count     1000.000000
mean       894.093000
std       3297.063594
min     -23119.000000
25%         82.750000
50%        282.500000
75%        784.500000
max      53394.000000
Name: Profits, dtype: float64
>>> """
... >>> profits.describe()
... count     1000.000000
... mean       894.093000
... std       3297.063594
... min     -23119.000000
... 25%         82.750000
... 50%        282.500000
... 75%        784.500000
... max      53394.000000
... """"
  File "<stdin>", line 11
    """"
       ^
SyntaxError: EOL while scanning string literal
>>>
>>> ##c. 7 successful companies with highest revenues
... from pandas import DataFrame, read_csv
>>> import matplotlib.pyplot as plt
>>> import pandas as pd
>>>
... file = 'c:\\Users\\Rony\\Desktop\\PYTHON EXamples folder\\fortune1000.csv'
>>> df = pd.read_csv(file)
>>>
>>> sorting = df.sort_values(by=['Profits'])
>>> sorting.tail(7)
    Rank             Company              Sector                                  Industry           Location  Revenue  Profits  Employees
28    29           Citigroup          Financials                          Commercial Banks       New York, NY    88275    17242     231000
12    13             Verizon  Telecommunications                        Telecommunications       New York, NY   131620    17879     177700
85    86     Gilead Sciences         Health Care                           Pharmaceuticals    Foster City, CA    32639    18108       8000
26    27         Wells Fargo          Financials                          Commercial Banks  San Francisco, CA    90033    22894     264700
3      4  Berkshire Hathaway          Financials  Insurance: Property and Casualty (Stock)          Omaha, NE   210821    24083     331000
22    23   J.P. Morgan Chase          Financials                          Commercial Banks       New York, NY   101006    24442     234598
2      3               Apple          Technology               Computers, Office Equipment      Cupertino, CA   233715    53394     110000
>>> """
... >>> sorting.tail(7)
...     Rank             Company              Sector                                  Industry           Location  Revenue  Profits  Employees
... 28    29           Citigroup          Financials                          Commercial Banks       New York, NY    88275    17242     231000
... 12    13             Verizon  Telecommunications                        Telecommunications       New York, NY   131620    17879     177700
... 85    86     Gilead Sciences         Health Care                           Pharmaceuticals    Foster City, CA    32639    18108       8000
... 26    27         Wells Fargo          Financials                          Commercial Banks  San Francisco, CA    90033    22894     264700
... 3      4  Berkshire Hathaway          Financials  Insurance: Property and Casualty (Stock)          Omaha, NE   210821    24083     331000
... 22    23   J.P. Morgan Chase          Financials                          Commercial Banks       New York, NY   101006    24442     234598
... 2      3               Apple          Technology               Computers, Office Equipment      Cupertino, CA   233715    53394     110000
... >>>
... """
'\n>>> sorting.tail(7)\n    Rank             Company              Sector                                  Industry           Location  Revenue  Profits  Employees\n28    29           Citigroup          Financials
                   Commercial Banks       New York, NY    88275    17242     231000\n12    13             Verizon  Telecommunications                        Telecommunications       New York, NY   131620    17879     177700\n85    86     Gilead Sciences         Health Care                           Pharmaceuticals    Foster City, CA    32639    18108       8000\n26    27         Wells Fargo          Financials
 Commercial Banks  San Francisco, CA    90033    22894     264700\n3      4  Berkshire Hathaway          Financials  Insurance: Property and Casualty (Stock)          Omaha, NE   210821    24083     331000\n22    23   J.P. Morgan Chase          Financials                          Commercial Banks       New York, NY   101006    24442     234598\n2      3               Apple          Technology               Computers, Office Equipment
     Cupertino, CA   233715    53394     110000\n>>>\n'
>>> ##d. finding the firms with number of employees higher than 300,000
...
>>> df1 = df
>>> df1.loc[df1.Employees<300000]='NA'
>>> #print(df1)
...  #filter out rows ina . dataframe with column Employees values NA/NAN
... df1_not_NA = df1[df1.Employees.notnull()]
>>> print(df1_not_NA)    #####לא מצליחה לסנן בהדפסה
    Rank                   Company                Sector                                  Industry         Location Revenue Profits Employees
0      1                   Walmart             Retailing                     General Merchandisers  Bentonville, AR  482130   14694   2300000
1     NA                        NA                    NA                                        NA               NA      NA      NA        NA
2     NA                        NA                    NA                                        NA               NA      NA      NA        NA
3      4        Berkshire Hathaway            Financials  Insurance: Property and Casualty (Stock)        Omaha, NE  210821   24083    331000
4     NA                        NA                    NA                                        NA               NA      NA      NA        NA
5     NA                        NA                    NA                                        NA               NA      NA      NA        NA
6     NA                        NA                    NA                                        NA               NA      NA      NA        NA
7     NA                        NA                    NA                                        NA               NA      NA      NA        NA
8     NA                        NA                    NA                                        NA               NA      NA      NA        NA
9     NA                        NA                    NA                                        NA               NA      NA      NA        NA
10    11          General Electric           Industrials                      Industrial Machinery    Fairfield, CT  140389   -6126    333000
11    NA                        NA                    NA                                        NA               NA      NA      NA        NA
12    NA                        NA                    NA                                        NA               NA      NA      NA        NA
13    NA                        NA                    NA                                        NA               NA      NA      NA        NA
14    NA                        NA                    NA                                        NA               NA      NA      NA        NA
15    NA                        NA                    NA                                        NA               NA      NA      NA        NA
16    17                    Kroger  Food and Drug Stores                      Food and Drug Stores   Cincinnati, OH  109830    2039    431000
17    NA                        NA                    NA                                        NA               NA      NA      NA        NA
18    19  Walgreens Boots Alliance  Food and Drug Stores                      Food and Drug Stores    Deerfield, IL  103444    4220    302500
19    NA                        NA                    NA                                        NA               NA      NA      NA        NA
20    NA                        NA                    NA                                        NA               NA      NA      NA        NA
21    NA                        NA                    NA                                        NA               NA      NA      NA        NA
22    NA                        NA                    NA                                        NA               NA      NA      NA        NA
23    NA                        NA                    NA                                        NA               NA      NA      NA        NA
24    NA                        NA                    NA                                        NA               NA      NA      NA        NA
25    NA                        NA                    NA                                        NA               NA      NA      NA        NA
26    NA                        NA                    NA                                        NA               NA      NA      NA        NA
27    28                Home Depot             Retailing                Specialty Retailers: Other      Atlanta, GA   88519    7009    385000
28    NA                        NA                    NA                                        NA               NA      NA      NA        NA
29    NA                        NA                    NA                                        NA               NA      NA      NA        NA
..   ...                       ...                   ...                                       ...              ...     ...     ...       ...
970   NA                        NA                    NA                                        NA               NA      NA      NA        NA
971   NA                        NA                    NA                                        NA               NA      NA      NA        NA
972   NA                        NA                    NA                                        NA               NA      NA      NA        NA
973   NA                        NA                    NA                                        NA               NA      NA      NA        NA
974   NA                        NA                    NA                                        NA               NA      NA      NA        NA
975   NA                        NA                    NA                                        NA               NA      NA      NA        NA
976   NA                        NA                    NA                                        NA               NA      NA      NA        NA
977   NA                        NA                    NA                                        NA               NA      NA      NA        NA
978   NA                        NA                    NA                                        NA               NA      NA      NA        NA
979   NA                        NA                    NA                                        NA               NA      NA      NA        NA
980   NA                        NA                    NA                                        NA               NA      NA      NA        NA
981   NA                        NA                    NA                                        NA               NA      NA      NA        NA
982   NA                        NA                    NA                                        NA               NA      NA      NA        NA
983   NA                        NA                    NA                                        NA               NA      NA      NA        NA
984   NA                        NA                    NA                                        NA               NA      NA      NA        NA
985   NA                        NA                    NA                                        NA               NA      NA      NA        NA
986   NA                        NA                    NA                                        NA               NA      NA      NA        NA
987   NA                        NA                    NA                                        NA               NA      NA      NA        NA
988   NA                        NA                    NA                                        NA               NA      NA      NA        NA
989   NA                        NA                    NA                                        NA               NA      NA      NA        NA
990   NA                        NA                    NA                                        NA               NA      NA      NA        NA
991   NA                        NA                    NA                                        NA               NA      NA      NA        NA
992   NA                        NA                    NA                                        NA               NA      NA      NA        NA
993   NA                        NA                    NA                                        NA               NA      NA      NA        NA
994   NA                        NA                    NA                                        NA               NA      NA      NA        NA
995   NA                        NA                    NA                                        NA               NA      NA      NA        NA
996   NA                        NA                    NA                                        NA               NA      NA      NA        NA
997   NA                        NA                    NA                                        NA               NA      NA      NA        NA
998   NA                        NA                    NA                                        NA               NA      NA      NA        NA
999   NA                        NA                    NA                                        NA               NA      NA      NA        NA

[1000 rows x 8 columns]
>>>
>>>
... ###e . number of sectors and number of industries
... ###for that we need to groupby sector and industries first
...
>>> df.groupby(['Sector']).agg(sum)
                                                                           Rank                        ...                                                                  Employees
Sector                                                                                                 ...
Financials                                                                    4                        ...                                                                     331000
Food and Drug Stores                                                         36                        ...                                                                     733500
Hotels, Resturants & Leisure                                                327                        ...                                                                     925000
Industrials                                                                  11                        ...                                                                     333000
NA                            NANANANANANANANANANANANANANANANANANANANANANANA...                        ...                          NANANANANANANANANANANANANANANANANANANANANANANA...
Retailing                                                                    67                        ...                                                                    3026000
Technology                                                                   31                        ...                                                                     411798
Transportation                                                              106                        ...                                                                     664275

[8 rows x 7 columns]
>>>
>>> """
... >>> df.groupby(['Sector']).agg(sum)
...                                                                            Rank                        ...                                                                  Employees
... Sector                                                                                                 ...
... Financials                                                                    4                        ...                                                                     331000
... Food and Drug Stores                                                         36                        ...                                                                     733500
... Hotels, Resturants & Leisure                                                327                        ...                                                                     925000
... Industrials                                                                  11                        ...                                                                     333000
... N/A                           N/AN/AN/AN/AN/AN/AN/AN/AN/AN/AN/AN/AN/AN/AN/AN...                        ...                          N/AN/AN/AN/AN/AN/AN/AN/AN/AN/AN/AN/AN/AN/AN/AN...
... Retailing                                                                    67                        ...                                                                    3026000
... Technology                                                                   31                        ...                                                                     411798
... Transportation                                                              106                        ...                                                                     664275
...
... [8 rows x 7 columns]
... """"
  File "<stdin>", line 15
    """"
       ^
SyntaxError: EOL while scanning string literal
>>>
>>> df.groupby(['Industry']).agg(sum)
                                                                                       Rank                        ...                                                                  Employees
Industry                                                                                                           ...
Food Services                                                                           327                        ...                                                                     925000
Food and Drug Stores                                                                     36                        ...                                                                     733500
General Merchandisers                                                                    39                        ...                                                                    2641000
Industrial Machinery                                                                     11                        ...                                                                     333000
Information Technology Services                                                          31                        ...                                                                     411798
Insurance: Property and Casualty (Stock)                                                  4                        ...                                                                     331000
Mail, Package, and Freight Delivery                                                     106                        ...                                                                     664275
NA                                        NANANANANANANANANANANANANANANANANANANANANANANA...                        ...                          NANANANANANANANANANANANANANANANANANANANANANANA...
Specialty Retailers: Other                                                               28                        ...                                                                     385000

[9 rows x 7 columns]
>>>
>>> """
... >>> df.groupby(['Industry']).agg(sum)
...                                                                                        Rank                        ...                                                                  Employees
... Industry                                                                                                           ...
... Food Services                                                                           327                        ...                                                                     925000
... Food and Drug Stores                                                                     36                        ...                                                                     733500
... General Merchandisers                                                                    39                        ...                                                                    2641000
... Industrial Machinery                                                                     11                        ...                                                                     333000
... Information Technology Services                                                          31                        ...                                                                     411798
... Insurance: Property and Casualty (Stock)                                                  4                        ...                                                                     331000
... Mail, Package, and Freight Delivery                                                     106                        ...                                                                     664275
... N/A                                       N/AN/AN/AN/AN/AN/AN/AN/AN/AN/AN/AN/AN/AN/AN/AN...                        ...                          N/AN/AN/AN/AN/AN/AN/AN/AN/AN/AN/AN/AN/AN/AN/AN...
... Specialty Retailers: Other                                                               28                        ...                                                                     385000
...
... [9 rows x 7 columns]
... """"
  File "<stdin>", line 16
    """"
       ^
SyntaxError: EOL while scanning string literal
>>>
>>> #results are 7 sectors and 8 industries
... #
... # #f. how many companies each industry has and what are 5 most popular industries
... from pandas import DataFrame, read_csv
>>> import matplotlib.pyplot as plt
>>> import pandas as pd
>>>
>>>
>>> file = 'c:\\Users\\Rony\\Desktop\\PYTHON EXamples folder\\fortune1000.csv'
>>> df = pd.read_csv(file)
>>>
... df2 = df.groupby(['Industry','Company'])['Location'].agg('sum')
>>> df2.count()
996
>>>
>>> df.groupby(['Industry','Company','Location'])['Rank'].agg(['sum','count'])
                                                                                                     sum  count
Industry                                      Company                       Location
Advertising, marketing                        Interpublic Group             New York, NY             355      1
                                              Omnicom Group                 New York, NY             186      1
Aerospace and Defense                         B/E Aerospace                 Wellington, FL           785      1
                                              Boeing                        Chicago, IL               24      1
                                              Curtiss-Wright                Charlotte, NC            903      1
                                              Delta Tucker Holdings         McLean, VA               987      1
                                              General Dynamics              Falls Church, VA          88      1
                                              Huntington Ingalls Industries Newport News, VA         378      1
                                              L-3 Communications            New York, NY             245      1
                                              Lockheed Martin               Bethesda, MD              60      1
                                              Moog                          Elma, NY                 836      1
                                              Northrop Grumman              Falls Church, VA         118      1
                                              Orbital ATK                   Dulles, VA               560      1
                                              Precision Castparts           Portland, OR             282      1
                                              Raytheon                      Waltham, MA              120      1
                                              Rockwell Collins              Cedar Rapids, IA         490      1
                                              Spirit AeroSystems Holdings   Wichita, KS              389      1
                                              Textron                       Providence, RI           209      1
                                              TransDigm Group               Cleveland, OH            788      1
                                              Triumph Group                 Berwyn, PA               605      1
                                              United Technologies           Farmington, CT            45      1
                                              Woodward                      Fort Collins, CO         958      1
Airlines                                      Alaska Air Group              Seattle, WA              459      1
                                              American Airlines Group       Fort Worth, TX            67      1
                                              Delta Air Lines               Atlanta, GA               68      1
                                              Hawaiian Holdings             Honolulu, HI             884      1
                                              JetBlue Airways               Long Island City, NY     405      1
                                              SkyWest                       St. George, UT           709      1
                                              Southwest Airlines            Dallas, TX               142      1
                                              Spirit Airlines               Miramar, FL              933      1
...                                                                                                  ...    ...
Wholesalers: Diversified                      NOW                           Houston, TX              727      1
                                              Nexeo Solutions Holdings      The Woodlands, TX        599      1
                                              Pool                          Covington, LA            875      1
                                              Sprague Resources             Portsmouth, NH           653      1
                                              VWR                           Radnor, PA               564      1
                                              Veritiv                       Atlanta, GA              323      1
                                              W.W. Grainger                 Lake Forest, IL          285      1
                                              WESCO International           Pittsburgh, PA           357      1
                                              Watsco                        Miami, FL                581      1
                                              World Fuel Services           Miami, FL                 92      1
Wholesalers: Electronics and Office Equipment Arrow Electronics             Centennial, CO           119      1
                                              Avnet                         Phoenix, AZ              102      1
                                              Essendant                     Deerfield, IL            477      1
                                              Ingram Micro                  Irvine, CA                64      1
                                              Insight Enterprises           Tempe, AZ                474      1
                                              ScanSource                    Greenville, SC           685      1
                                              Synnex                        Fremont, CA              212      1
                                              Tech Data                     Clearwater, FL           108      1
Wholesalers: Food and Grocery                 Core-Mark Holding             South San Francisco, CA  317      1
                                              Performance Food Group        Richmond, VA             185      1
                                              SpartanNash                   Byron Center, MI         351      1
                                              Sysco                         Houston, TX               57      1
                                              US Foods Holding              Rosemont, IL             122      1
                                              United Natural Foods          Providence, RI           335      1
Wholesalers: Health Care                      AmerisourceBergen             Chesterbrook, PA          12      1
                                              Cardinal Health               Dublin, OH                21      1
                                              Henry Schein                  Melville, NY             268      1
                                              McKesson                      San Francisco, CA          5      1
                                              Owens & Minor                 Mechanicsville, VA       291      1
                                              Patterson                     St. Paul, MN             559      1

[996 rows x 2 columns]
>>>
>>> df.groupby(['Industry','Company'])['Location'].count().reset_index()
                                          Industry                        Company  Location
0                           Advertising, marketing              Interpublic Group         1
1                           Advertising, marketing                  Omnicom Group         1
2                            Aerospace and Defense                  B/E Aerospace         1
3                            Aerospace and Defense                         Boeing         1
4                            Aerospace and Defense                 Curtiss-Wright         1
5                            Aerospace and Defense          Delta Tucker Holdings         1
6                            Aerospace and Defense               General Dynamics         1
7                            Aerospace and Defense  Huntington Ingalls Industries         1
8                            Aerospace and Defense             L-3 Communications         1
9                            Aerospace and Defense                Lockheed Martin         1
10                           Aerospace and Defense                           Moog         1
11                           Aerospace and Defense               Northrop Grumman         1
12                           Aerospace and Defense                    Orbital ATK         1
13                           Aerospace and Defense            Precision Castparts         1
14                           Aerospace and Defense                       Raytheon         1
15                           Aerospace and Defense               Rockwell Collins         1
16                           Aerospace and Defense    Spirit AeroSystems Holdings         1
17                           Aerospace and Defense                        Textron         1
18                           Aerospace and Defense                TransDigm Group         1
19                           Aerospace and Defense                  Triumph Group         1
20                           Aerospace and Defense            United Technologies         1
21                           Aerospace and Defense                       Woodward         1
22                                        Airlines               Alaska Air Group         1
23                                        Airlines        American Airlines Group         1
24                                        Airlines                Delta Air Lines         1
25                                        Airlines              Hawaiian Holdings         1
26                                        Airlines                JetBlue Airways         1
27                                        Airlines                        SkyWest         1
28                                        Airlines             Southwest Airlines         1
29                                        Airlines                Spirit Airlines         1
..                                             ...                            ...       ...
966                       Wholesalers: Diversified                            NOW         1
967                       Wholesalers: Diversified       Nexeo Solutions Holdings         1
968                       Wholesalers: Diversified                           Pool         1
969                       Wholesalers: Diversified              Sprague Resources         1
970                       Wholesalers: Diversified                            VWR         1
971                       Wholesalers: Diversified                        Veritiv         1
972                       Wholesalers: Diversified                  W.W. Grainger         1
973                       Wholesalers: Diversified            WESCO International         1
974                       Wholesalers: Diversified                         Watsco         1
975                       Wholesalers: Diversified            World Fuel Services         1
976  Wholesalers: Electronics and Office Equipment              Arrow Electronics         1
977  Wholesalers: Electronics and Office Equipment                          Avnet         1
978  Wholesalers: Electronics and Office Equipment                      Essendant         1
979  Wholesalers: Electronics and Office Equipment                   Ingram Micro         1
980  Wholesalers: Electronics and Office Equipment            Insight Enterprises         1
981  Wholesalers: Electronics and Office Equipment                     ScanSource         1
982  Wholesalers: Electronics and Office Equipment                         Synnex         1
983  Wholesalers: Electronics and Office Equipment                      Tech Data         1
984                  Wholesalers: Food and Grocery              Core-Mark Holding         1
985                  Wholesalers: Food and Grocery         Performance Food Group         1
986                  Wholesalers: Food and Grocery                    SpartanNash         1
987                  Wholesalers: Food and Grocery                          Sysco         1
988                  Wholesalers: Food and Grocery               US Foods Holding         1
989                  Wholesalers: Food and Grocery           United Natural Foods         1
990                       Wholesalers: Health Care              AmerisourceBergen         1
991                       Wholesalers: Health Care                Cardinal Health         1
992                       Wholesalers: Health Care                   Henry Schein         1
993                       Wholesalers: Health Care                       McKesson         1
994                       Wholesalers: Health Care                  Owens & Minor         1
995                       Wholesalers: Health Care                      Patterson         1

[996 rows x 3 columns]
>>> df.groupby(['Industry','Company']).agg('count').reset_index()
                                          Industry                        Company  Rank  Sector  Location  Revenue  Profits  Employees
0                           Advertising, marketing              Interpublic Group     1       1         1        1        1          1
1                           Advertising, marketing                  Omnicom Group     1       1         1        1        1          1
2                            Aerospace and Defense                  B/E Aerospace     1       1         1        1        1          1
3                            Aerospace and Defense                         Boeing     1       1         1        1        1          1
4                            Aerospace and Defense                 Curtiss-Wright     1       1         1        1        1          1
5                            Aerospace and Defense          Delta Tucker Holdings     1       1         1        1        1          1
6                            Aerospace and Defense               General Dynamics     1       1         1        1        1          1
7                            Aerospace and Defense  Huntington Ingalls Industries     1       1         1        1        1          1
8                            Aerospace and Defense             L-3 Communications     1       1         1        1        1          1
9                            Aerospace and Defense                Lockheed Martin     1       1         1        1        1          1
10                           Aerospace and Defense                           Moog     1       1         1        1        1          1
11                           Aerospace and Defense               Northrop Grumman     1       1         1        1        1          1
12                           Aerospace and Defense                    Orbital ATK     1       1         1        1        1          1
13                           Aerospace and Defense            Precision Castparts     1       1         1        1        1          1
14                           Aerospace and Defense                       Raytheon     1       1         1        1        1          1
15                           Aerospace and Defense               Rockwell Collins     1       1         1        1        1          1
16                           Aerospace and Defense    Spirit AeroSystems Holdings     1       1         1        1        1          1
17                           Aerospace and Defense                        Textron     1       1         1        1        1          1
18                           Aerospace and Defense                TransDigm Group     1       1         1        1        1          1
19                           Aerospace and Defense                  Triumph Group     1       1         1        1        1          1
20                           Aerospace and Defense            United Technologies     1       1         1        1        1          1
21                           Aerospace and Defense                       Woodward     1       1         1        1        1          1
22                                        Airlines               Alaska Air Group     1       1         1        1        1          1
23                                        Airlines        American Airlines Group     1       1         1        1        1          1
24                                        Airlines                Delta Air Lines     1       1         1        1        1          1
25                                        Airlines              Hawaiian Holdings     1       1         1        1        1          1
26                                        Airlines                JetBlue Airways     1       1         1        1        1          1
27                                        Airlines                        SkyWest     1       1         1        1        1          1
28                                        Airlines             Southwest Airlines     1       1         1        1        1          1
29                                        Airlines                Spirit Airlines     1       1         1        1        1          1
..                                             ...                            ...   ...     ...       ...      ...      ...        ...
966                       Wholesalers: Diversified                            NOW     1       1         1        1        1          1
967                       Wholesalers: Diversified       Nexeo Solutions Holdings     1       1         1        1        1          1
968                       Wholesalers: Diversified                           Pool     1       1         1        1        1          1
969                       Wholesalers: Diversified              Sprague Resources     1       1         1        1        1          1
970                       Wholesalers: Diversified                            VWR     1       1         1        1        1          1
971                       Wholesalers: Diversified                        Veritiv     1       1         1        1        1          1
972                       Wholesalers: Diversified                  W.W. Grainger     1       1         1        1        1          1
973                       Wholesalers: Diversified            WESCO International     1       1         1        1        1          1
974                       Wholesalers: Diversified                         Watsco     1       1         1        1        1          1
975                       Wholesalers: Diversified            World Fuel Services     1       1         1        1        1          1
976  Wholesalers: Electronics and Office Equipment              Arrow Electronics     1       1         1        1        1          1
977  Wholesalers: Electronics and Office Equipment                          Avnet     1       1         1        1        1          1
978  Wholesalers: Electronics and Office Equipment                      Essendant     1       1         1        1        1          1
979  Wholesalers: Electronics and Office Equipment                   Ingram Micro     1       1         1        1        1          1
980  Wholesalers: Electronics and Office Equipment            Insight Enterprises     1       1         1        1        1          1
981  Wholesalers: Electronics and Office Equipment                     ScanSource     1       1         1        1        1          1
982  Wholesalers: Electronics and Office Equipment                         Synnex     1       1         1        1        1          1
983  Wholesalers: Electronics and Office Equipment                      Tech Data     1       1         1        1        1          1
984                  Wholesalers: Food and Grocery              Core-Mark Holding     1       1         1        1        1          1
985                  Wholesalers: Food and Grocery         Performance Food Group     1       1         1        1        1          1
986                  Wholesalers: Food and Grocery                    SpartanNash     1       1         1        1        1          1
987                  Wholesalers: Food and Grocery                          Sysco     1       1         1        1        1          1
988                  Wholesalers: Food and Grocery               US Foods Holding     1       1         1        1        1          1
989                  Wholesalers: Food and Grocery           United Natural Foods     1       1         1        1        1          1
990                       Wholesalers: Health Care              AmerisourceBergen     1       1         1        1        1          1
991                       Wholesalers: Health Care                Cardinal Health     1       1         1        1        1          1
992                       Wholesalers: Health Care                   Henry Schein     1       1         1        1        1          1
993                       Wholesalers: Health Care                       McKesson     1       1         1        1        1          1
994                       Wholesalers: Health Care                  Owens & Minor     1       1         1        1        1          1
995                       Wholesalers: Health Care                      Patterson     1       1         1        1        1          1

[996 rows x 8 columns]
>>>
>>>
>>> #g. splitting the location onto two columns
...
>>> from pandas import DataFrame, read_csv
>>> import matplotlib.pyplot as plt
>>> import pandas as pd
>>>
>>>
>>> file = 'c:\\Users\\Rony\\Desktop\\PYTHON EXamples folder\\fortune1000.csv'
>>> df = pd.read_csv(file)
>>>
>>>
>>> fortune1000['City'],fortune1000['State'] =fortune1000.Location.str.split(',').str
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'fortune1000' is not defined
>>>
>>> print(df)
     Rank                            Company                        Sector                                    Industry           Location  Revenue  Profits  Employees
0       1                            Walmart                     Retailing                       General Merchandisers    Bentonville, AR   482130    14694    2300000
1       2                        Exxon Mobil                        Energy                          Petroleum Refining         Irving, TX   246204    16150      75600
2       3                              Apple                    Technology                 Computers, Office Equipment      Cupertino, CA   233715    53394     110000
3       4                 Berkshire Hathaway                    Financials    Insurance: Property and Casualty (Stock)          Omaha, NE   210821    24083     331000
4       5                           McKesson                   Health Care                    Wholesalers: Health Care  San Francisco, CA   181241     1476      70400
5       6                 UnitedHealth Group                   Health Care     Health Care: Insurance and Managed Care     Minnetonka, MN   157107     5813     200000
6       7                         CVS Health          Food and Drug Stores                        Food and Drug Stores     Woonsocket, RI   153290     5237     199000
7       8                     General Motors        Motor Vehicles & Parts                    Motor Vehicles and Parts        Detroit, MI   152356     9687     215000
8       9                         Ford Motor        Motor Vehicles & Parts                    Motor Vehicles and Parts       Dearborn, MI   149558     7373     199000
9      10                               AT&T            Telecommunications                          Telecommunications         Dallas, TX   146801    13345     281450
10     11                   General Electric                   Industrials                        Industrial Machinery      Fairfield, CT   140389    -6126     333000
11     12                  AmerisourceBergen                   Health Care                    Wholesalers: Health Care   Chesterbrook, PA   135962     -135      17000
12     13                            Verizon            Telecommunications                          Telecommunications       New York, NY   131620    17879     177700
13     14                            Chevron                        Energy                          Petroleum Refining      San Ramon, CA   131118     4587      61500
14     15                             Costco                     Retailing                  Specialty Retailers: Other       Issaquah, WA   116199     2377     161000
15     16                         Fannie Mae                    Financials                      Diversified Financials     Washington, DC   110359    10954       7300
16     17                             Kroger          Food and Drug Stores                        Food and Drug Stores     Cincinnati, OH   109830     2039     431000
17     18                         Amazon.com                    Technology             Internet Services and Retailing        Seattle, WA   107006      596     230800
18     19           Walgreens Boots Alliance          Food and Drug Stores                        Food and Drug Stores      Deerfield, IL   103444     4220     302500
19     20                                 HP                    Technology                 Computers, Office Equipment      Palo Alto, CA   103355     4554     287000
20     21                    Cardinal Health                   Health Care                    Wholesalers: Health Care         Dublin, OH   102531     1215      34500
21     22            Express Scripts Holding                   Health Care    Health Care: Pharmacy and Other Services      St. Louis, MO   101752     2476      25900
22     23                  J.P. Morgan Chase                    Financials                            Commercial Banks       New York, NY   101006    24442     234598
23     24                             Boeing           Aerospace & Defense                       Aerospace and Defense        Chicago, IL    96114     5176     161400
24     25                          Microsoft                    Technology                           Computer Software        Redmond, WA    93580    12193     118000
25     26              Bank of America Corp.                    Financials                            Commercial Banks      Charlotte, NC    93056    15888     213280
26     27                        Wells Fargo                    Financials                            Commercial Banks  San Francisco, CA    90033    22894     264700
27     28                         Home Depot                     Retailing                  Specialty Retailers: Other        Atlanta, GA    88519     7009     385000
28     29                          Citigroup                    Financials                            Commercial Banks       New York, NY    88275    17242     231000
29     30                        Phillips 66                        Energy                          Petroleum Refining        Houston, TX    87169     4227      14000
..    ...                                ...                           ...                                         ...                ...      ...      ...        ...
970   971                   VeriFone Systems                    Technology                 Computers, Office Equipment       San Jose, CA     2001       79       5400
971   972                  Genesee & Wyoming                Transportation                                   Railroads         Darien, CT     2000      225       7500
972   973               99 Cents Only Stores                     Retailing                  Specialty Retailers: Other       Commerce, CA     1999     -232      18200
973   974  Roadrunner Transportation Systems                Transportation                Transportation and Logistics         Cudahy, WI     1995       48       4502
974   975               Super Micro Computer                    Technology                 Computers, Office Equipment       San Jose, CA     1991      102       2285
975   976                First Republic Bank                    Financials                            Commercial Banks  San Francisco, CA     1989      522       3130
976   977                  Hill-Rom Holdings                   Health Care              Medical Products and Equipment        Chicago, IL     1988       48      10000
977   978                 Providence Service                   Health Care    Health Care: Pharmacy and Other Services         Tucson, AZ     1987       84       9072
978   979      Allison Transmission Holdings        Motor Vehicles & Parts                    Motor Vehicles and Parts   Indianaoplis, IN     1986      182       2700
979   980                              Spire                        Energy                 Utilities: Gas and Electric      St. Louis, MO     1976      137       3078
980   981                         WPX Energy                        Energy                Mining, Crude-Oil Production          Tulsa, OK     1958    -1727       1040
981   982                   Century Aluminum                     Materials                                      Metals        Chicago, IL     1950      -59       1778
982   983           Adams Resources & Energy                        Energy                          Petroleum Refining        Houston, TX     1944       -1        809
983   984              Nuance Communications                    Technology                           Computer Software     Burlington, MA     1931     -115      13500
984   985                  Primoris Services    Engineering & Construction                   Engineering, Construction         Dallas, TX     1929       37       7011
985   986         Schnitzer Steel Industries                     Materials                                      Metals       Portland, OR     1924     -197       2955
986   987              Delta Tucker Holdings           Aerospace & Defense                       Aerospace and Defense         McLean, VA     1923     -133      12000
987   988       Hospitality Properties Trust                    Financials                                 Real estate         Newton, MA     1922      166        400
988   989                             Cenveo                         Media                        Publishing, Printing       Stamford, CT     1921      -31       7300
989   990                        F5 Networks                    Technology  Network and Other Communications Equipment        Seattle, WA     1920      365       4178
990   991                  BlueLinx Holdings                   Wholesalers                    Wholesalers: Diversified        Atlanta, GA     1917      -12       1600
991   992                             Revlon            Household Products             Household and Personal Products       New York, NY     1914       56       5700
992   993              DeVry Education Group             Business Services                                   Education  Downers Grove, IL     1910      140      11770
993   994                       MDC Holdings    Engineering & Construction                                Homebuilders         Denver, CO     1909       66       1225
994   995                          EP Energy                        Energy                Mining, Crude-Oil Production        Houston, TX     1908    -3748        665
995   996         New York Community Bancorp                    Financials                            Commercial Banks       Westbury, NY     1902      -47       3448
996   997          Portland General Electric                        Energy                 Utilities: Gas and Electric       Portland, OR     1898      172       2646
997   997          Portland General Electric                        Energy                 Utilities: Gas and Electric       Portland, OR     1898      172       2646
998   999                            Wendys  Hotels, Resturants & Leisure                               Food Services         Dublin, OH     1896      161      21200
999  1000                  Briggs & Stratton                   Industrials                        Industrial Machinery      Wauwatosa, WI     1895       46       5480

[1000 rows x 8 columns]
>>>
>>>
>>> """"
... example to partial output
...
... 9>> print(df)
...      Rank                            Company                        Sector                                    Industry           Location  Revenue  Profits  Employees           City State
... 0       1                            Walmart                     Retailing                       General Merchandisers    Bentonville, AR   482130    14694    2300000    Bentonville    AR
... 1       2                        Exxon Mobil                        Energy                          Petroleum Refining         Irving, TX   246204    16150      75600         Irving    TX
... 2       3                              Apple                    Technology                 Computers, Office Equipment      Cupertino, CA   233715    53394     110000      Cupertino    CA
... 3       4                 Berkshire Hathaway                    Financials    Insurance: Property and Casualty (Stock)          Omaha, NE   210821    24083     331000          Omaha    NE
... 4       5                           McKesson                   Health Care                    Wholesalers: Health Care  San Francisco, CA   181241     1476      70400  San Francisco    CA
... 5       6                 UnitedHealth Group                   Health Care     Health Care: Insurance and Managed Care     Minnetonka, MN   157107     5813     200000     Minnetonka    MN
... 6       7                         CVS Health          Food and Drug Stores                        Food and Drug Stores     Woonsocket, RI   153290     5237     199000     Woonsocket    RI
... 7       8                     General Motors        Motor Vehicles & Parts                    Motor Vehicles and Parts        Detroit, MI   152356     9687     215000        Detroit    MI
... 8       9                         Ford Motor        Motor Vehicles & Parts                    Motor Vehicles and Parts       Dearborn, MI   149558     7373     199000       Dearborn    MI
... 9      10                               AT&T            Telecommunications                          Telecommunications         Dallas, TX   146801    13345     281450         Dallas    TX
... 10     11                   General Electric                   Industrials                        Industrial Machinery      Fairfield, CT   140389    -6126     333000      Fairfield    CT
... 11     12                  AmerisourceBergen                   Health Care                    Wholesalers: Health Care   Chesterbrook, PA   135962     -135      17000   Chesterbrook    PA
... 12     13                            Verizon            Telecommunications                          Telecommunications       New York, NY   131620    17879     177700       New York    NY
... 13     14                            Chevron                        Energy                          Petroleum Refining      San Ramon, CA   131118     4587      61500      San Ramon    CA
... 14     15                             Costco                     Retailing                  Specialty Retailers: Other       Issaquah, WA   116199     2377     161000       Issaquah    WA
... 15     16                         Fannie Mae                    Financials                      Diversified Financials     Washington, DC   110359    10954       7300     Washington    DC
... 16     17                             Kroger          Food and Drug Stores                        Food and Drug Stores     Cincinnati, OH   109830     2039     431000     Cincinnati    OH
... 17     18                         Amazon.com                    Technology             Internet Services and Retailing        Seattle, WA   107006      596     230800        Seattle    WA
... 18     19           Walgreens Boots Alliance          Food and Drug Stores                        Food and Drug Stores      Deerfield, IL   103444     4220     302500      Deerfield    IL
... 19     20                                 HP                    Technology                 Computers, Office Equipment      Palo Alto, CA   103355     4554     287000      Palo Alto    CA
... 20     21                    Cardinal Health                   Health Care                    Wholesalers: Health Care         Dublin, OH   102531     1215      34500         Dublin    OH
... 21     22            Express Scripts Holding                   Health Care    Health Care: Pharmacy and Other Services      St. Louis, MO   101752     2476      25900      St. Louis    MO
... 22     23                  J.P. Morgan Chase                    Financials                            Commercial Banks       New York, NY   101006    24442     234598       New York    NY
... 23     24                             Boeing           Aerospace & Defense                       Aerospace and Defense        Chicago, IL    96114     5176     161400        Chicago    IL
... 24     25                          Microsoft                    Technology                           Computer Software        Redmond, WA    93580    12193     118000        Redmond    WA
... 25     26              Bank of America Corp.                    Financials                            Commercial Banks      Charlotte, NC    93056    15888     213280      Charlotte    NC
... 26     27                        Wells Fargo                    Financials                            Commercial Banks  San Francisco, CA    90033    22894     264700  San Francisco    CA
... 27     28                         Home Depot                     Retailing                  Specialty Retailers: Other        Atlanta, GA    88519     7009     385000        Atlanta    GA
... 28     29                          Citigroup                    Financials                            Commercial Banks       New York, NY    88275    17242     231000       New York    NY
... 29     30                        Phillips 66                        Energy                          Petroleum Refining        Houston, TX    87169     4227      14000        Houston    TX
... ..    ...                                ...                           ...                                         ...                ...      ...      ...        ...            ...   ...
... 970   971                   VeriFone Systems                    Technology                 Computers, Office Equipment       San Jose, CA     2001       79       5400       San Jose    CA
... 971   972                  Genesee & Wyoming                Transportation                                   Railroads         Darien, CT     2000      225       7500         Darien    CT
... 972   973               99 Cents Only Stores                     Retailing                  Specialty Retailers: Other       Commerce, CA     1999     -232      18200       Commerce    CA
... 973   974  Roadrunner Transportation Systems                Transportation                Transportation and Logistics         Cudahy, WI     1995       48       4502         Cudahy    WI
... 974   975               Super Micro Computer                    Technology                 Computers, Office Equipment       San Jose, CA     1991      102       2285       San Jose    CA
... 975   976                First Republic Bank                    Financials                            Commercial Banks  San Francisco, CA     1989      522       3130  San Francisco    CA
... 976   977                  Hill-Rom Holdings                   Health Care              Medical Products and Equipment        Chicago, IL     1988       48      10000        Chicago    IL
... 977   978                 Providence Service                   Health Care    Health Care: Pharmacy and Other Services         Tucson, AZ     1987       84       9072         Tucson    AZ
... 978   979      Allison Transmission Holdings        Motor Vehicles & Parts                    Motor Vehicles and Parts   Indianaoplis, IN     1986      182       2700   Indianaoplis    IN
... 979   980                              Spire                        Energy                 Utilities: Gas and Electric      St. Louis, MO     1976      137       3078      St. Louis    MO
... 980   981                         WPX Energy                        Energy                Mining, Crude-Oil Production          Tulsa, OK     1958    -1727       1040          Tulsa    OK
... 981   982                   Century Aluminum                     Materials                                      Metals        Chicago, IL     1950      -59       1778        Chicago    IL
... 982   983           Adams Resources & Energy                        Energy                          Petroleum Refining        Houston, TX     1944       -1        809        Houston    TX
... 983   984              Nuance Communications                    Technology                           Computer Software     Burlington, MA     1931     -115      13500     Burlington    MA
... 984   985                  Primoris Services    Engineering & Construction                   Engineering, Construction         Dallas, TX     1929       37       7011         Dallas    TX
... 985   986         Schnitzer Steel Industries                     Materials                                      Metals       Portland, OR     1924     -197       2955       Portland    OR
... 986   987              Delta Tucker Holdings           Aerospace & Defense                       Aerospace and Defense         McLean, VA     1923     -133      12000         McLean    VA
... 987   988       Hospitality Properties Trust                    Financials                                 Real estate         Newton, MA     1922      166        400         Newton    MA
... 988   989                             Cenveo                         Media                        Publishing, Printing       Stamford, CT     1921      -31       7300       Stamford    CT
... 989   990                        F5 Networks                    Technology  Network and Other Communications Equipment        Seattle, WA     1920      365       4178        Seattle    WA
... 990   991                  BlueLinx Holdings                   Wholesalers                    Wholesalers: Diversified        Atlanta, GA     1917      -12       1600        Atlanta    GA
... 991   992                             Revlon            Household Products             Household and Personal Products       New York, NY     1914       56       5700       New York    NY
... 992   993              DeVry Education Group             Business Services                                   Education  Downers Grove, IL     1910      140      11770  Downers Grove    IL
... 993   994                       MDC Holdings    Engineering & Construction                                Homebuilders         Denver, CO     1909       66       1225         Denver    CO
... 994   995                          EP Energy                        Energy                Mining, Crude-Oil Production        Houston, TX     1908    -3748        665        Houston    TX
... 995   996         New York Community Bancorp                    Financials                            Commercial Banks       Westbury, NY     1902      -47       3448       Westbury    NY
... 996   997          Portland General Electric                        Energy                 Utilities: Gas and Electric       Portland, OR     1898      172       2646       Portland    OR
... 997   997          Portland General Electric                        Energy                 Utilities: Gas and Electric       Portland, OR     1898      172       2646       Portland    OR
... 998   999                            Wendy’s  Hotels, Resturants & Leisure                               Food Services         Dublin, OH     1896      161      21200         Dublin    OH
... 999  1000                  Briggs & Stratton                   Industrials                        Industrial Machinery      Wauwatosa, WI     1895       46       5480      Wauwatosa    WI
...
... [1000 rows x 10 columns]
... >>>
... >>>
... """"
  File "<stdin>", line 71
    """"
      ^
SyntaxError: EOL while scanning string literal
>>> #### how many states represented and which is the most common
... y = df.State
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "C:\Users\Rony\Miniconda3\lib\site-packages\pandas\core\generic.py", line 4376, in __getattr__
    return object.__getattribute__(self, name)
AttributeError: 'DataFrame' object has no attribute 'State'
>>> y.describe()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'RangeIndex' object has no attribute 'describe'
>>> """
... >>> y.describe()
... count     1000
... unique      45
... top         CA
... freq       108
... Name: State, dtype: object
... there are just 45 states represented and CA is the freq.
... """
'\n>>> y.describe()\ncount     1000\nunique      45\ntop         CA\nfreq       108\nName: State, dtype: object\nthere are just 45 states represented and CA is the freq. \n'
>>>
>>> ###h. companies located in NY rank smaller than 50
... from pandas import DataFrame, read_csv
>>> import matplotlib.pyplot as plt
>>> import pandas as pd
>>>
>>>
>>> file = 'c:\\Users\\Rony\\Desktop\\PYTHON EXamples folder\\fortune1000.csv'
>>> df = pd.read_csv(file)
>>> fortune1000['City'],fortune1000['State'] =fortune1000.Location.str.split(',').str
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'fortune1000' is not defined
>>>
>>>
>>> df.loc[df['Rank'] < 50, 'State'] = 'NY'
>>> print(df)
     Rank                            Company                        Sector                                    Industry           Location  Revenue  Profits  Employees State
0       1                            Walmart                     Retailing                       General Merchandisers    Bentonville, AR   482130    14694    2300000    NY
1       2                        Exxon Mobil                        Energy                          Petroleum Refining         Irving, TX   246204    16150      75600    NY
2       3                              Apple                    Technology                 Computers, Office Equipment      Cupertino, CA   233715    53394     110000    NY
3       4                 Berkshire Hathaway                    Financials    Insurance: Property and Casualty (Stock)          Omaha, NE   210821    24083     331000    NY
4       5                           McKesson                   Health Care                    Wholesalers: Health Care  San Francisco, CA   181241     1476      70400    NY
5       6                 UnitedHealth Group                   Health Care     Health Care: Insurance and Managed Care     Minnetonka, MN   157107     5813     200000    NY
6       7                         CVS Health          Food and Drug Stores                        Food and Drug Stores     Woonsocket, RI   153290     5237     199000    NY
7       8                     General Motors        Motor Vehicles & Parts                    Motor Vehicles and Parts        Detroit, MI   152356     9687     215000    NY
8       9                         Ford Motor        Motor Vehicles & Parts                    Motor Vehicles and Parts       Dearborn, MI   149558     7373     199000    NY
9      10                               AT&T            Telecommunications                          Telecommunications         Dallas, TX   146801    13345     281450    NY
10     11                   General Electric                   Industrials                        Industrial Machinery      Fairfield, CT   140389    -6126     333000    NY
11     12                  AmerisourceBergen                   Health Care                    Wholesalers: Health Care   Chesterbrook, PA   135962     -135      17000    NY
12     13                            Verizon            Telecommunications                          Telecommunications       New York, NY   131620    17879     177700    NY
13     14                            Chevron                        Energy                          Petroleum Refining      San Ramon, CA   131118     4587      61500    NY
14     15                             Costco                     Retailing                  Specialty Retailers: Other       Issaquah, WA   116199     2377     161000    NY
15     16                         Fannie Mae                    Financials                      Diversified Financials     Washington, DC   110359    10954       7300    NY
16     17                             Kroger          Food and Drug Stores                        Food and Drug Stores     Cincinnati, OH   109830     2039     431000    NY
17     18                         Amazon.com                    Technology             Internet Services and Retailing        Seattle, WA   107006      596     230800    NY
18     19           Walgreens Boots Alliance          Food and Drug Stores                        Food and Drug Stores      Deerfield, IL   103444     4220     302500    NY
19     20                                 HP                    Technology                 Computers, Office Equipment      Palo Alto, CA   103355     4554     287000    NY
20     21                    Cardinal Health                   Health Care                    Wholesalers: Health Care         Dublin, OH   102531     1215      34500    NY
21     22            Express Scripts Holding                   Health Care    Health Care: Pharmacy and Other Services      St. Louis, MO   101752     2476      25900    NY
22     23                  J.P. Morgan Chase                    Financials                            Commercial Banks       New York, NY   101006    24442     234598    NY
23     24                             Boeing           Aerospace & Defense                       Aerospace and Defense        Chicago, IL    96114     5176     161400    NY
24     25                          Microsoft                    Technology                           Computer Software        Redmond, WA    93580    12193     118000    NY
25     26              Bank of America Corp.                    Financials                            Commercial Banks      Charlotte, NC    93056    15888     213280    NY
26     27                        Wells Fargo                    Financials                            Commercial Banks  San Francisco, CA    90033    22894     264700    NY
27     28                         Home Depot                     Retailing                  Specialty Retailers: Other        Atlanta, GA    88519     7009     385000    NY
28     29                          Citigroup                    Financials                            Commercial Banks       New York, NY    88275    17242     231000    NY
29     30                        Phillips 66                        Energy                          Petroleum Refining        Houston, TX    87169     4227      14000    NY
..    ...                                ...                           ...                                         ...                ...      ...      ...        ...   ...
970   971                   VeriFone Systems                    Technology                 Computers, Office Equipment       San Jose, CA     2001       79       5400   NaN
971   972                  Genesee & Wyoming                Transportation                                   Railroads         Darien, CT     2000      225       7500   NaN
972   973               99 Cents Only Stores                     Retailing                  Specialty Retailers: Other       Commerce, CA     1999     -232      18200   NaN
973   974  Roadrunner Transportation Systems                Transportation                Transportation and Logistics         Cudahy, WI     1995       48       4502   NaN
974   975               Super Micro Computer                    Technology                 Computers, Office Equipment       San Jose, CA     1991      102       2285   NaN
975   976                First Republic Bank                    Financials                            Commercial Banks  San Francisco, CA     1989      522       3130   NaN
976   977                  Hill-Rom Holdings                   Health Care              Medical Products and Equipment        Chicago, IL     1988       48      10000   NaN
977   978                 Providence Service                   Health Care    Health Care: Pharmacy and Other Services         Tucson, AZ     1987       84       9072   NaN
978   979      Allison Transmission Holdings        Motor Vehicles & Parts                    Motor Vehicles and Parts   Indianaoplis, IN     1986      182       2700   NaN
979   980                              Spire                        Energy                 Utilities: Gas and Electric      St. Louis, MO     1976      137       3078   NaN
980   981                         WPX Energy                        Energy                Mining, Crude-Oil Production          Tulsa, OK     1958    -1727       1040   NaN
981   982                   Century Aluminum                     Materials                                      Metals        Chicago, IL     1950      -59       1778   NaN
982   983           Adams Resources & Energy                        Energy                          Petroleum Refining        Houston, TX     1944       -1        809   NaN
983   984              Nuance Communications                    Technology                           Computer Software     Burlington, MA     1931     -115      13500   NaN
984   985                  Primoris Services    Engineering & Construction                   Engineering, Construction         Dallas, TX     1929       37       7011   NaN
985   986         Schnitzer Steel Industries                     Materials                                      Metals       Portland, OR     1924     -197       2955   NaN
986   987              Delta Tucker Holdings           Aerospace & Defense                       Aerospace and Defense         McLean, VA     1923     -133      12000   NaN
987   988       Hospitality Properties Trust                    Financials                                 Real estate         Newton, MA     1922      166        400   NaN
988   989                             Cenveo                         Media                        Publishing, Printing       Stamford, CT     1921      -31       7300   NaN
989   990                        F5 Networks                    Technology  Network and Other Communications Equipment        Seattle, WA     1920      365       4178   NaN
990   991                  BlueLinx Holdings                   Wholesalers                    Wholesalers: Diversified        Atlanta, GA     1917      -12       1600   NaN
991   992                             Revlon            Household Products             Household and Personal Products       New York, NY     1914       56       5700   NaN
992   993              DeVry Education Group             Business Services                                   Education  Downers Grove, IL     1910      140      11770   NaN
993   994                       MDC Holdings    Engineering & Construction                                Homebuilders         Denver, CO     1909       66       1225   NaN
994   995                          EP Energy                        Energy                Mining, Crude-Oil Production        Houston, TX     1908    -3748        665   NaN
995   996         New York Community Bancorp                    Financials                            Commercial Banks       Westbury, NY     1902      -47       3448   NaN
996   997          Portland General Electric                        Energy                 Utilities: Gas and Electric       Portland, OR     1898      172       2646   NaN
997   997          Portland General Electric                        Energy                 Utilities: Gas and Electric       Portland, OR     1898      172       2646   NaN
998   999                            Wendy’s  Hotels, Resturants & Leisure                               Food Services         Dublin, OH     1896      161      21200   NaN
999  1000                  Briggs & Stratton                   Industrials                        Industrial Machinery      Wauwatosa, WI     1895       46       5480   NaN

[1000 rows x 9 columns]
>>>
>>> """"
... question h
... df.loc[df['Rank'] < 50, 'State'] = 'NY'
... print(df)
... >>> print(df)
...      Rank                            Company                        Sector                                    Industry           Location  Revenue  Profits  Employees State
... 0       1                            Walmart                     Retailing                       General Merchandisers    Bentonville, AR   482130    14694    2300000    NY
... 1       2                        Exxon Mobil                        Energy                          Petroleum Refining         Irving, TX   246204    16150      75600    NY
... 2       3                              Apple                    Technology                 Computers, Office Equipment      Cupertino, CA   233715    53394     110000    NY
... 3       4                 Berkshire Hathaway                    Financials    Insurance: Property and Casualty (Stock)          Omaha, NE   210821    24083     331000    NY
... 4       5                           McKesson                   Health Care                    Wholesalers: Health Care  San Francisco, CA   181241     1476      70400    NY
... 5       6                 UnitedHealth Group                   Health Care     Health Care: Insurance and Managed Care     Minnetonka, MN   157107     5813     200000    NY
... 6       7                         CVS Health          Food and Drug Stores                        Food and Drug Stores     Woonsocket, RI   153290     5237     199000    NY
... 7       8                     General Motors        Motor Vehicles & Parts                    Motor Vehicles and Parts        Detroit, MI   152356     9687     215000    NY
... 8       9                         Ford Motor        Motor Vehicles & Parts                    Motor Vehicles and Parts       Dearborn, MI   149558     7373     199000    NY
... 9      10                               AT&T            Telecommunications                          Telecommunications         Dallas, TX   146801    13345     281450    NY
... 10     11                   General Electric                   Industrials                        Industrial Machinery      Fairfield, CT   140389    -6126     333000    NY
... 11     12                  AmerisourceBergen                   Health Care                    Wholesalers: Health Care   Chesterbrook, PA   135962     -135      17000    NY
... 12     13                            Verizon            Telecommunications                          Telecommunications       New York, NY   131620    17879     177700    NY
... 13     14                            Chevron                        Energy                          Petroleum Refining      San Ramon, CA   131118     4587      61500    NY
... 14     15                             Costco                     Retailing                  Specialty Retailers: Other       Issaquah, WA   116199     2377     161000    NY
... 15     16                         Fannie Mae                    Financials                      Diversified Financials     Washington, DC   110359    10954       7300    NY
... 16     17                             Kroger          Food and Drug Stores                        Food and Drug Stores     Cincinnati, OH   109830     2039     431000    NY
... 17     18                         Amazon.com                    Technology             Internet Services and Retailing        Seattle, WA   107006      596     230800    NY
... 18     19           Walgreens Boots Alliance          Food and Drug Stores                        Food and Drug Stores      Deerfield, IL   103444     4220     302500    NY
... 19     20                                 HP                    Technology                 Computers, Office Equipment      Palo Alto, CA   103355     4554     287000    NY
... 20     21                    Cardinal Health                   Health Care                    Wholesalers: Health Care         Dublin, OH   102531     1215      34500    NY
... 21     22            Express Scripts Holding                   Health Care    Health Care: Pharmacy and Other Services      St. Louis, MO   101752     2476      25900    NY
... 22     23                  J.P. Morgan Chase                    Financials                            Commercial Banks       New York, NY   101006    24442     234598    NY
... 23     24                             Boeing           Aerospace & Defense                       Aerospace and Defense        Chicago, IL    96114     5176     161400    NY
... 24     25                          Microsoft                    Technology                           Computer Software        Redmond, WA    93580    12193     118000    NY
... 25     26              Bank of America Corp.                    Financials                            Commercial Banks      Charlotte, NC    93056    15888     213280    NY
... 26     27                        Wells Fargo                    Financials                            Commercial Banks  San Francisco, CA    90033    22894     264700    NY
... 27     28                         Home Depot                     Retailing                  Specialty Retailers: Other        Atlanta, GA    88519     7009     385000    NY
... 28     29                          Citigroup                    Financials                            Commercial Banks       New York, NY    88275    17242     231000    NY
... 29     30                        Phillips 66                        Energy                          Petroleum Refining        Houston, TX    87169     4227      14000    NY
... ..    ...
... """"
  File "<stdin>", line 38
    """"
       ^
SyntaxError: EOL while scanning string literal
>>>
>>> #question i - sectors assessment
... file = 'c:\\Users\\Rony\\Desktop\\PYTHON EXamples folder\\fortune1000.csv'
>>> df = pd.read_csv(file)
>>>
>>> #i built a function of means to learn on the average
... df.groupby('Sector').mean()
                                    Rank       Revenue      Profits     Employees
Sector
Aerospace & Defense           443.500000  17897.000000  1437.100000  48402.850000
Apparel                       583.800000   6397.866667   549.066667  23093.133333
Business Services             609.294118   5337.156863   553.470588  26687.254902
Chemicals                     530.933333   8129.900000   754.266667  15455.033333
Energy                        509.827869  12441.057377  -602.024590   9745.303279
Engineering & Construction    582.576923   5922.423077   204.000000  15642.615385
Financials                    457.309353  15950.784173  1872.007194  24172.287770
Food and Drug Stores          428.600000  32251.266667  1117.266667  93026.533333
Food, Beverages & Tobacco     433.232558  12929.465116  1195.744186  28177.488372
Health Care                   423.600000  21529.426667  1414.853333  35710.520000
Hotels, Resturants & Leisure  561.560000   6781.840000   827.880000  99369.800000
Household Products            562.464286   8383.464286   515.285714  23072.785714
Industrials                   586.695652  10816.978261   451.391304  33591.934783
Materials                     542.860465   6026.627907   102.976744  14840.069767
Media                         559.040000   8830.560000   973.880000  22012.560000
Motor Vehicles & Parts        475.333333  20105.833333  1079.083333  45106.666667
Retailing                     445.637500  18313.450000   597.875000  77845.362500
Technology                    557.862745  13505.882353  1769.343137  35087.735294
Telecommunications            325.133333  30788.933333  3242.466667  55497.866667
Transportation                509.694444  11347.444444  1226.916667  42688.694444
Wholesalers                   421.125000  11120.000000   205.825000  13139.925000
>>>
>>> """"
... I have this table now :
... >>> df.groupby('Sector').mean()
...                                     Rank       Revenue      Profits     Employees
... Sector
... Aerospace & Defense           443.500000  17897.000000  1437.100000  48402.850000
... Apparel                       583.800000   6397.866667   549.066667  23093.133333
... Business Services             609.294118   5337.156863   553.470588  26687.254902
... Chemicals                     530.933333   8129.900000   754.266667  15455.033333
... Energy                        509.827869  12441.057377  -602.024590   9745.303279
... Engineering & Construction    582.576923   5922.423077   204.000000  15642.615385
... Financials                    457.309353  15950.784173  1872.007194  24172.287770
... Food and Drug Stores          428.600000  32251.266667  1117.266667  93026.533333
... Food, Beverages & Tobacco     433.232558  12929.465116  1195.744186  28177.488372
... Health Care                   423.600000  21529.426667  1414.853333  35710.520000
... Hotels, Resturants & Leisure  561.560000   6781.840000   827.880000  99369.800000
... Household Products            562.464286   8383.464286   515.285714  23072.785714
... Industrials                   586.695652  10816.978261   451.391304  33591.934783
... Materials                     542.860465   6026.627907   102.976744  14840.069767
... Media                         559.040000   8830.560000   973.880000  22012.560000
... Motor Vehicles & Parts        475.333333  20105.833333  1079.083333  45106.666667
... Retailing                     445.637500  18313.450000   597.875000  77845.362500
... Technology                    557.862745  13505.882353  1769.343137  35087.735294
... Telecommunications            325.133333  30788.933333  3242.466667  55497.866667
... Transportation                509.694444  11347.444444  1226.916667  42688.694444
... Wholesalers                   421.125000  11120.000000   205.825000  13139.925000
... >>>
... """"
  File "<stdin>", line 28
    """"
       ^
SyntaxError: EOL while scanning string literal
>>> #First I am checking the function of the profits
... y = df.groupby('Sector').mean()
>>> y.Profits.describe()
count      21.000000
mean      928.032108
std       792.832162
min      -602.024590
25%       515.285714
50%       827.880000
75%      1226.916667
max      3242.466667
Name: Profits, dtype: float64
>>> """
... >>> y.Profits.describe()
... count      21.000000
... mean      928.032108
... std       792.832162
... min      -602.024590   i will use that to find the industry energy
... 25%       515.285714
... 50%       827.880000
... 75%      1226.916667
... max      3242.466667     i will use that to find the industry  telecomunication
... Name: Profits, dtype: float64
... >>>
... """"
  File "<stdin>", line 13
    """"
       ^
SyntaxError: EOL while scanning string literal
>>>
>>>
>>> y.loc[['Profits']]['Sector'] == 3242.466667
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\Rony\Miniconda3\lib\site-packages\pandas\core\indexing.py", line 1478, in __getitem__
    return self._getitem_axis(maybe_callable, axis=axis)
  File "C:\Users\Rony\Miniconda3\lib\site-packages\pandas\core\indexing.py", line 1901, in _getitem_axis
    return self._getitem_iterable(key, axis=axis)
  File "C:\Users\Rony\Miniconda3\lib\site-packages\pandas\core\indexing.py", line 1143, in _getitem_iterable
    self._validate_read_indexer(key, indexer, axis)
  File "C:\Users\Rony\Miniconda3\lib\site-packages\pandas\core\indexing.py", line 1206, in _validate_read_indexer
    key=key, axis=self.obj._get_axis_name(axis)))
KeyError: "None of [['Profits']] are in the [index]"
>>> print(y)
                                    Rank       Revenue      Profits     Employees
Sector
Aerospace & Defense           443.500000  17897.000000  1437.100000  48402.850000
Apparel                       583.800000   6397.866667   549.066667  23093.133333
Business Services             609.294118   5337.156863   553.470588  26687.254902
Chemicals                     530.933333   8129.900000   754.266667  15455.033333
Energy                        509.827869  12441.057377  -602.024590   9745.303279
Engineering & Construction    582.576923   5922.423077   204.000000  15642.615385
Financials                    457.309353  15950.784173  1872.007194  24172.287770
Food and Drug Stores          428.600000  32251.266667  1117.266667  93026.533333
Food, Beverages & Tobacco     433.232558  12929.465116  1195.744186  28177.488372
Health Care                   423.600000  21529.426667  1414.853333  35710.520000
Hotels, Resturants & Leisure  561.560000   6781.840000   827.880000  99369.800000
Household Products            562.464286   8383.464286   515.285714  23072.785714
Industrials                   586.695652  10816.978261   451.391304  33591.934783
Materials                     542.860465   6026.627907   102.976744  14840.069767
Media                         559.040000   8830.560000   973.880000  22012.560000
Motor Vehicles & Parts        475.333333  20105.833333  1079.083333  45106.666667
Retailing                     445.637500  18313.450000   597.875000  77845.362500
Technology                    557.862745  13505.882353  1769.343137  35087.735294
Telecommunications            325.133333  30788.933333  3242.466667  55497.866667
Transportation                509.694444  11347.444444  1226.916667  42688.694444
Wholesalers                   421.125000  11120.000000   205.825000  13139.925000
>>>

This snippet took 0.09 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).