我在下面的数据帧nbr2:

    Postal_Code     Borough     Neighborhood
0   M1B     Scarborough     Rouge, Malvern
1   M4C     East York   Woodbine Heights
2   M4E     East Toronto    The Beaches
3   M4L     East Toronto    The Beaches West, India Bazaar
4   M4M     East Toronto    Studio District
5   M4N     Central Toronto     Lawrence Park


在应用以下代码以过滤出行时:

neighbor = nbr2.drop(nbr2[nbr2['Borough'].str.contains("Toronto")==False].index, axis=0, inplace=True)


数据框的分布如下:

  Postal_Code           Borough  \
 37         M4E      East Toronto
 41         M4K      East Toronto
 42         M4L      East Toronto
 43         M4M      East Toronto
                                         Neighborhood
37                                        The Beaches
41                     The Danforth West\n, Riverdale
42                   The Beaches West\n, India Bazaar
43                                  Studio District\n


下面的代码也导致类似的结构:

# define the dataframe columns
column_names = ['Postal_Code','Borough', 'Neighborhood']
# instantiate the dataframe
neighbor = pd.DataFrame(columns=column_names)

neighbor = nbr2.drop(nbr2[nbr2['Borough'].str.contains("Toronto")==False].index, axis=0, inplace=True)

最佳答案

采用

pd.set_option('display.expand_frame_repr', False)

10-07 14:48