Pandas - insert, pop, copy, drop/truncate, concat, drop_duplicate

Soogyung Gwon·2026년 2월 2일

구름을잡아라

목록 보기
12/60
import pandas as pd

df = pd.read_csv('./data/data_cts_prisons_and_prisoners.csv')
print(df.head())
  Iso3_code                            Country    Region  \
0       BRB                           Barbados  Americas   
1       CRI                         Costa Rica  Americas   
2       DMA                           Dominica  Americas   
3    GBR_NI  United Kingdom (Northern Ireland)    Europe   
4       ITA                              Italy    Europe   

                         Subregion                Indicator  \
0  Latin America and the Caribbean  Persons entering prison   
1  Latin America and the Caribbean  Persons entering prison   
2  Latin America and the Caribbean  Persons entering prison   
3                  Northern Europe  Persons entering prison   
4                  Southern Europe  Persons entering prison   

           Dimension              Category    Sex    Age  Year  \
0  by selected crime  Intentional Homicide  Total  Total  2016   
1  by selected crime  Intentional Homicide  Total  Total  2016   
2  by selected crime  Intentional Homicide  Total  Total  2016   
3  by selected crime  Intentional Homicide  Total  Total  2016   
4  by selected crime  Intentional Homicide  Total  Total  2016   

  Unit of measurement   VALUE Source  
0              Counts     0.0    CTS  
1              Counts   173.0    CTS  
2              Counts    65.0    CTS  
3              Counts    61.0    CTS  
4              Counts  1443.0    CTS  
item = df.pop('Country')
print(df)
      Iso3_code    Region                        Subregion  \
0           BRB  Americas  Latin America and the Caribbean   
1           CRI  Americas  Latin America and the Caribbean   
2           DMA  Americas  Latin America and the Caribbean   
3        GBR_NI    Europe                  Northern Europe   
4           ITA    Europe                  Southern Europe   
...         ...       ...                              ...   
84020       ZWE    Africa               Sub-Saharan Africa   
84021       ZWE    Africa               Sub-Saharan Africa   
84022       ZWE    Africa               Sub-Saharan Africa   
84023       ZWE    Africa               Sub-Saharan Africa   
84024       ZWE    Africa               Sub-Saharan Africa   

                     Indicator          Dimension              Category  \
0      Persons entering prison  by selected crime  Intentional Homicide   
1      Persons entering prison  by selected crime  Intentional Homicide   
2      Persons entering prison  by selected crime  Intentional Homicide   
3      Persons entering prison  by selected crime  Intentional Homicide   
4      Persons entering prison  by selected crime  Intentional Homicide   
...                        ...                ...                   ...   
84020             Persons held              Total                 Total   
84021             Persons held              Total                 Total   
84022             Persons held              Total                 Total   
84023             Persons held              Total                 Total   
84024             Persons held              Total                 Total   

          Sex    Age  Year          Unit of measurement        VALUE Source  
0       Total  Total  2016                       Counts     0.000000    CTS  
1       Total  Total  2016                       Counts   173.000000    CTS  
2       Total  Total  2016                       Counts    65.000000    CTS  
3       Total  Total  2016                       Counts    61.000000    CTS  
4       Total  Total  2016                       Counts  1443.000000    CTS  
...       ...    ...   ...                          ...          ...    ...  
84020  Female  Total  2021  Rate per 100,000 population     5.197952    CTS  
84021    Male  Total  2022  Rate per 100,000 population   296.818546    CTS  
84022  Female  Total  2022  Rate per 100,000 population     7.049325    CTS  
84023    Male  Total  2023  Rate per 100,000 population   281.238749    CTS  
84024  Female  Total  2023  Rate per 100,000 population     7.663652    CTS  

[84025 rows x 12 columns]
item = df.pop('Indicator')
print(df)
      Iso3_code    Region                        Subregion          Dimension  \
0           BRB  Americas  Latin America and the Caribbean  by selected crime   
1           CRI  Americas  Latin America and the Caribbean  by selected crime   
2           DMA  Americas  Latin America and the Caribbean  by selected crime   
3        GBR_NI    Europe                  Northern Europe  by selected crime   
4           ITA    Europe                  Southern Europe  by selected crime   
...         ...       ...                              ...                ...   
84020       ZWE    Africa               Sub-Saharan Africa              Total   
84021       ZWE    Africa               Sub-Saharan Africa              Total   
84022       ZWE    Africa               Sub-Saharan Africa              Total   
84023       ZWE    Africa               Sub-Saharan Africa              Total   
84024       ZWE    Africa               Sub-Saharan Africa              Total   

                   Category     Sex    Age  Year          Unit of measurement  \
0      Intentional Homicide   Total  Total  2016                       Counts   
1      Intentional Homicide   Total  Total  2016                       Counts   
2      Intentional Homicide   Total  Total  2016                       Counts   
3      Intentional Homicide   Total  Total  2016                       Counts   
4      Intentional Homicide   Total  Total  2016                       Counts   
...                     ...     ...    ...   ...                          ...   
84020                 Total  Female  Total  2021  Rate per 100,000 population   
84021                 Total    Male  Total  2022  Rate per 100,000 population   
84022                 Total  Female  Total  2022  Rate per 100,000 population   
84023                 Total    Male  Total  2023  Rate per 100,000 population   
84024                 Total  Female  Total  2023  Rate per 100,000 population   

             VALUE Source  
0         0.000000    CTS  
1       173.000000    CTS  
2        65.000000    CTS  
3        61.000000    CTS  
4      1443.000000    CTS  
...            ...    ...  
84020     5.197952    CTS  
84021   296.818546    CTS  
84022     7.049325    CTS  
84023   281.238749    CTS  
84024     7.663652    CTS  

[84025 rows x 11 columns]
deep = df.copy(deep=True)
shallow = df.copy(deep=False)
df['Region'] = 'origin changed'
shallow['Subregion'] = 'changed'
print(shallow)
print(f'-------------------------------------------------\n{df}')
      Iso3_code          Region Subregion          Dimension  \
0           BRB  origin changed   changed  by selected crime   
1           CRI  origin changed   changed  by selected crime   
2           DMA  origin changed   changed  by selected crime   
3        GBR_NI  origin changed   changed  by selected crime   
4           ITA  origin changed   changed  by selected crime   
...         ...             ...       ...                ...   
84020       ZWE  origin changed   changed              Total   
84021       ZWE  origin changed   changed              Total   
84022       ZWE  origin changed   changed              Total   
84023       ZWE  origin changed   changed              Total   
84024       ZWE  origin changed   changed              Total   

                   Category     Sex    Age  Year          Unit of measurement  \
0      Intentional Homicide   Total  Total  2016                       Counts   
1      Intentional Homicide   Total  Total  2016                       Counts   
2      Intentional Homicide   Total  Total  2016                       Counts   
3      Intentional Homicide   Total  Total  2016                       Counts   
4      Intentional Homicide   Total  Total  2016                       Counts   
...                     ...     ...    ...   ...                          ...   
84020                 Total  Female  Total  2021  Rate per 100,000 population   
84021                 Total    Male  Total  2022  Rate per 100,000 population   
84022                 Total  Female  Total  2022  Rate per 100,000 population   
84023                 Total    Male  Total  2023  Rate per 100,000 population   
84024                 Total  Female  Total  2023  Rate per 100,000 population   

             VALUE Source  
0         0.000000    CTS  
1       173.000000    CTS  
2        65.000000    CTS  
3        61.000000    CTS  
4      1443.000000    CTS  
...            ...    ...  
84020     5.197952    CTS  
84021   296.818546    CTS  
84022     7.049325    CTS  
84023   281.238749    CTS  
84024     7.663652    CTS  

[84025 rows x 11 columns]
-------------------------------------------------
      Iso3_code          Region                        Subregion  \
0           BRB  origin changed  Latin America and the Caribbean   
1           CRI  origin changed  Latin America and the Caribbean   
2           DMA  origin changed  Latin America and the Caribbean   
3        GBR_NI  origin changed                  Northern Europe   
4           ITA  origin changed                  Southern Europe   
...         ...             ...                              ...   
84020       ZWE  origin changed               Sub-Saharan Africa   
84021       ZWE  origin changed               Sub-Saharan Africa   
84022       ZWE  origin changed               Sub-Saharan Africa   
84023       ZWE  origin changed               Sub-Saharan Africa   
84024       ZWE  origin changed               Sub-Saharan Africa   

               Dimension              Category     Sex    Age  Year  \
0      by selected crime  Intentional Homicide   Total  Total  2016   
1      by selected crime  Intentional Homicide   Total  Total  2016   
2      by selected crime  Intentional Homicide   Total  Total  2016   
3      by selected crime  Intentional Homicide   Total  Total  2016   
4      by selected crime  Intentional Homicide   Total  Total  2016   
...                  ...                   ...     ...    ...   ...   
84020              Total                 Total  Female  Total  2021   
84021              Total                 Total    Male  Total  2022   
84022              Total                 Total  Female  Total  2022   
84023              Total                 Total    Male  Total  2023   
84024              Total                 Total  Female  Total  2023   

               Unit of measurement        VALUE Source  
0                           Counts     0.000000    CTS  
1                           Counts   173.000000    CTS  
2                           Counts    65.000000    CTS  
3                           Counts    61.000000    CTS  
4                           Counts  1443.000000    CTS  
...                            ...          ...    ...  
84020  Rate per 100,000 population     5.197952    CTS  
84021  Rate per 100,000 population   296.818546    CTS  
84022  Rate per 100,000 population     7.049325    CTS  
84023  Rate per 100,000 population   281.238749    CTS  
84024  Rate per 100,000 population     7.663652    CTS  

[84025 rows x 11 columns]
print(df.drop(labels=2, axis=0))
      Iso3_code          Region                        Subregion  \
0           BRB  origin changed  Latin America and the Caribbean   
1           CRI  origin changed  Latin America and the Caribbean   
3        GBR_NI  origin changed                  Northern Europe   
4           ITA  origin changed                  Southern Europe   
5           LTU  origin changed                  Northern Europe   
...         ...             ...                              ...   
84020       ZWE  origin changed               Sub-Saharan Africa   
84021       ZWE  origin changed               Sub-Saharan Africa   
84022       ZWE  origin changed               Sub-Saharan Africa   
84023       ZWE  origin changed               Sub-Saharan Africa   
84024       ZWE  origin changed               Sub-Saharan Africa   

               Dimension              Category     Sex    Age  Year  \
0      by selected crime  Intentional Homicide   Total  Total  2016   
1      by selected crime  Intentional Homicide   Total  Total  2016   
3      by selected crime  Intentional Homicide   Total  Total  2016   
4      by selected crime  Intentional Homicide   Total  Total  2016   
5      by selected crime  Intentional Homicide   Total  Total  2016   
...                  ...                   ...     ...    ...   ...   
84020              Total                 Total  Female  Total  2021   
84021              Total                 Total    Male  Total  2022   
84022              Total                 Total  Female  Total  2022   
84023              Total                 Total    Male  Total  2023   
84024              Total                 Total  Female  Total  2023   

               Unit of measurement        VALUE Source  
0                           Counts     0.000000    CTS  
1                           Counts   173.000000    CTS  
3                           Counts    61.000000    CTS  
4                           Counts  1443.000000    CTS  
5                           Counts   417.000000    CTS  
...                            ...          ...    ...  
84020  Rate per 100,000 population     5.197952    CTS  
84021  Rate per 100,000 population   296.818546    CTS  
84022  Rate per 100,000 population     7.049325    CTS  
84023  Rate per 100,000 population   281.238749    CTS  
84024  Rate per 100,000 population     7.663652    CTS  

[84024 rows x 11 columns]
print(df.drop(labels='Region', axis=1))
      Iso3_code                        Subregion          Dimension  \
0           BRB  Latin America and the Caribbean  by selected crime   
1           CRI  Latin America and the Caribbean  by selected crime   
2           DMA  Latin America and the Caribbean  by selected crime   
3        GBR_NI                  Northern Europe  by selected crime   
4           ITA                  Southern Europe  by selected crime   
...         ...                              ...                ...   
84020       ZWE               Sub-Saharan Africa              Total   
84021       ZWE               Sub-Saharan Africa              Total   
84022       ZWE               Sub-Saharan Africa              Total   
84023       ZWE               Sub-Saharan Africa              Total   
84024       ZWE               Sub-Saharan Africa              Total   

                   Category     Sex    Age  Year          Unit of measurement  \
0      Intentional Homicide   Total  Total  2016                       Counts   
1      Intentional Homicide   Total  Total  2016                       Counts   
2      Intentional Homicide   Total  Total  2016                       Counts   
3      Intentional Homicide   Total  Total  2016                       Counts   
4      Intentional Homicide   Total  Total  2016                       Counts   
...                     ...     ...    ...   ...                          ...   
84020                 Total  Female  Total  2021  Rate per 100,000 population   
84021                 Total    Male  Total  2022  Rate per 100,000 population   
84022                 Total  Female  Total  2022  Rate per 100,000 population   
84023                 Total    Male  Total  2023  Rate per 100,000 population   
84024                 Total  Female  Total  2023  Rate per 100,000 population   

             VALUE Source  
0         0.000000    CTS  
1       173.000000    CTS  
2        65.000000    CTS  
3        61.000000    CTS  
4      1443.000000    CTS  
...            ...    ...  
84020     5.197952    CTS  
84021   296.818546    CTS  
84022     7.049325    CTS  
84023   281.238749    CTS  
84024     7.663652    CTS  

[84025 rows x 10 columns]
print(df.drop(index=3))
      Iso3_code          Region                        Subregion  \
0           BRB  origin changed  Latin America and the Caribbean   
1           CRI  origin changed  Latin America and the Caribbean   
2           DMA  origin changed  Latin America and the Caribbean   
4           ITA  origin changed                  Southern Europe   
5           LTU  origin changed                  Northern Europe   
...         ...             ...                              ...   
84020       ZWE  origin changed               Sub-Saharan Africa   
84021       ZWE  origin changed               Sub-Saharan Africa   
84022       ZWE  origin changed               Sub-Saharan Africa   
84023       ZWE  origin changed               Sub-Saharan Africa   
84024       ZWE  origin changed               Sub-Saharan Africa   

               Dimension              Category     Sex    Age  Year  \
0      by selected crime  Intentional Homicide   Total  Total  2016   
1      by selected crime  Intentional Homicide   Total  Total  2016   
2      by selected crime  Intentional Homicide   Total  Total  2016   
4      by selected crime  Intentional Homicide   Total  Total  2016   
5      by selected crime  Intentional Homicide   Total  Total  2016   
...                  ...                   ...     ...    ...   ...   
84020              Total                 Total  Female  Total  2021   
84021              Total                 Total    Male  Total  2022   
84022              Total                 Total  Female  Total  2022   
84023              Total                 Total    Male  Total  2023   
84024              Total                 Total  Female  Total  2023   

               Unit of measurement        VALUE Source  
0                           Counts     0.000000    CTS  
1                           Counts   173.000000    CTS  
2                           Counts    65.000000    CTS  
4                           Counts  1443.000000    CTS  
5                           Counts   417.000000    CTS  
...                            ...          ...    ...  
84020  Rate per 100,000 population     5.197952    CTS  
84021  Rate per 100,000 population   296.818546    CTS  
84022  Rate per 100,000 population     7.049325    CTS  
84023  Rate per 100,000 population   281.238749    CTS  
84024  Rate per 100,000 population     7.663652    CTS  

[84024 rows x 11 columns]
print(df.drop(columns='VALUE'))
      Iso3_code          Region                        Subregion  \
0           BRB  origin changed  Latin America and the Caribbean   
1           CRI  origin changed  Latin America and the Caribbean   
2           DMA  origin changed  Latin America and the Caribbean   
3        GBR_NI  origin changed                  Northern Europe   
4           ITA  origin changed                  Southern Europe   
...         ...             ...                              ...   
84020       ZWE  origin changed               Sub-Saharan Africa   
84021       ZWE  origin changed               Sub-Saharan Africa   
84022       ZWE  origin changed               Sub-Saharan Africa   
84023       ZWE  origin changed               Sub-Saharan Africa   
84024       ZWE  origin changed               Sub-Saharan Africa   

               Dimension              Category     Sex    Age  Year  \
0      by selected crime  Intentional Homicide   Total  Total  2016   
1      by selected crime  Intentional Homicide   Total  Total  2016   
2      by selected crime  Intentional Homicide   Total  Total  2016   
3      by selected crime  Intentional Homicide   Total  Total  2016   
4      by selected crime  Intentional Homicide   Total  Total  2016   
...                  ...                   ...     ...    ...   ...   
84020              Total                 Total  Female  Total  2021   
84021              Total                 Total    Male  Total  2022   
84022              Total                 Total  Female  Total  2022   
84023              Total                 Total    Male  Total  2023   
84024              Total                 Total  Female  Total  2023   

               Unit of measurement Source  
0                           Counts    CTS  
1                           Counts    CTS  
2                           Counts    CTS  
3                           Counts    CTS  
4                           Counts    CTS  
...                            ...    ...  
84020  Rate per 100,000 population    CTS  
84021  Rate per 100,000 population    CTS  
84022  Rate per 100,000 population    CTS  
84023  Rate per 100,000 population    CTS  
84024  Rate per 100,000 population    CTS  

[84025 rows x 10 columns]
print(df.drop(labels=['Category', 'Not exist'],axis=1, errors='raise'))
---------------------------------------------------------------------------

KeyError                                  Traceback (most recent call last)

Cell In[107], line 1
----> 1 print(df.drop(labels=['Category', 'Not exist'],axis=1, errors='raise'))


File ~\AppData\Local\Programs\Python\Python314\Lib\site-packages\pandas\core\frame.py:5603, in DataFrame.drop(self, labels, axis, index, columns, level, inplace, errors)
   5455 def drop(
   5456     self,
   5457     labels: IndexLabel | None = None,
   (...)   5464     errors: IgnoreRaise = "raise",
   5465 ) -> DataFrame | None:
   5466     """
   5467     Drop specified labels from rows or columns.
   5468 
   (...)   5601             weight  1.0     0.8
   5602     """
-> 5603     return super().drop(
   5604         labels=labels,
   5605         axis=axis,
   5606         index=index,
   5607         columns=columns,
   5608         level=level,
   5609         inplace=inplace,
   5610         errors=errors,
   5611     )


File ~\AppData\Local\Programs\Python\Python314\Lib\site-packages\pandas\core\generic.py:4810, in NDFrame.drop(self, labels, axis, index, columns, level, inplace, errors)
   4808 for axis, labels in axes.items():
   4809     if labels is not None:
-> 4810         obj = obj._drop_axis(labels, axis, level=level, errors=errors)
   4812 if inplace:
   4813     self._update_inplace(obj)


File ~\AppData\Local\Programs\Python\Python314\Lib\site-packages\pandas\core\generic.py:4852, in NDFrame._drop_axis(self, labels, axis, level, errors, only_slice)
   4850         new_axis = axis.drop(labels, level=level, errors=errors)
   4851     else:
-> 4852         new_axis = axis.drop(labels, errors=errors)
   4853     indexer = axis.get_indexer(new_axis)
   4855 # Case for non-unique axis
   4856 else:


File ~\AppData\Local\Programs\Python\Python314\Lib\site-packages\pandas\core\indexes\base.py:7136, in Index.drop(self, labels, errors)
   7134 if mask.any():
   7135     if errors != "ignore":
-> 7136         raise KeyError(f"{labels[mask].tolist()} not found in axis")
   7137     indexer = indexer[~mask]
   7138 return self.delete(indexer)


KeyError: "['Category', 'Not exist'] not found in axis"
print(df.drop(labels=['Category', 'Not exist'],axis=1, errors='ignore'))
      Iso3_code          Region                        Subregion  \
0           BRB  origin changed  Latin America and the Caribbean   
1           CRI  origin changed  Latin America and the Caribbean   
2           DMA  origin changed  Latin America and the Caribbean   
3        GBR_NI  origin changed                  Northern Europe   
4           ITA  origin changed                  Southern Europe   
...         ...             ...                              ...   
84020       ZWE  origin changed               Sub-Saharan Africa   
84021       ZWE  origin changed               Sub-Saharan Africa   
84022       ZWE  origin changed               Sub-Saharan Africa   
84023       ZWE  origin changed               Sub-Saharan Africa   
84024       ZWE  origin changed               Sub-Saharan Africa   

               Dimension     Sex    Age  Year          Unit of measurement  \
0      by selected crime   Total  Total  2016                       Counts   
1      by selected crime   Total  Total  2016                       Counts   
2      by selected crime   Total  Total  2016                       Counts   
3      by selected crime   Total  Total  2016                       Counts   
4      by selected crime   Total  Total  2016                       Counts   
...                  ...     ...    ...   ...                          ...   
84020              Total  Female  Total  2021  Rate per 100,000 population   
84021              Total    Male  Total  2022  Rate per 100,000 population   
84022              Total  Female  Total  2022  Rate per 100,000 population   
84023              Total    Male  Total  2023  Rate per 100,000 population   
84024              Total  Female  Total  2023  Rate per 100,000 population   

             VALUE Source  
0         0.000000    CTS  
1       173.000000    CTS  
2        65.000000    CTS  
3        61.000000    CTS  
4      1443.000000    CTS  
...            ...    ...  
84020     5.197952    CTS  
84021   296.818546    CTS  
84022     7.049325    CTS  
84023   281.238749    CTS  
84024     7.663652    CTS  

[84025 rows x 10 columns]
print(df.drop(labels=['Category', 'Year'],axis=1, inplace=True))
None
print(df)
      Iso3_code          Region                        Subregion  \
0           BRB  origin changed  Latin America and the Caribbean   
1           CRI  origin changed  Latin America and the Caribbean   
2           DMA  origin changed  Latin America and the Caribbean   
3        GBR_NI  origin changed                  Northern Europe   
4           ITA  origin changed                  Southern Europe   
...         ...             ...                              ...   
84020       ZWE  origin changed               Sub-Saharan Africa   
84021       ZWE  origin changed               Sub-Saharan Africa   
84022       ZWE  origin changed               Sub-Saharan Africa   
84023       ZWE  origin changed               Sub-Saharan Africa   
84024       ZWE  origin changed               Sub-Saharan Africa   

               Dimension     Sex    Age          Unit of measurement  \
0      by selected crime   Total  Total                       Counts   
1      by selected crime   Total  Total                       Counts   
2      by selected crime   Total  Total                       Counts   
3      by selected crime   Total  Total                       Counts   
4      by selected crime   Total  Total                       Counts   
...                  ...     ...    ...                          ...   
84020              Total  Female  Total  Rate per 100,000 population   
84021              Total    Male  Total  Rate per 100,000 population   
84022              Total  Female  Total  Rate per 100,000 population   
84023              Total    Male  Total  Rate per 100,000 population   
84024              Total  Female  Total  Rate per 100,000 population   

             VALUE Source  
0         0.000000    CTS  
1       173.000000    CTS  
2        65.000000    CTS  
3        61.000000    CTS  
4      1443.000000    CTS  
...            ...    ...  
84020     5.197952    CTS  
84021   296.818546    CTS  
84022     7.049325    CTS  
84023   281.238749    CTS  
84024     7.663652    CTS  

[84025 rows x 9 columns]
print(df.truncate(before=4, after=84021, axis=0))
      Iso3_code          Region                        Subregion  \
4           ITA  origin changed                  Southern Europe   
5           LTU  origin changed                  Northern Europe   
6           MAC  origin changed                     Eastern Asia   
7           MEX  origin changed  Latin America and the Caribbean   
8           NOR  origin changed                  Northern Europe   
...         ...             ...                              ...   
84017       ZWE  origin changed               Sub-Saharan Africa   
84018       ZWE  origin changed               Sub-Saharan Africa   
84019       ZWE  origin changed               Sub-Saharan Africa   
84020       ZWE  origin changed               Sub-Saharan Africa   
84021       ZWE  origin changed               Sub-Saharan Africa   

               Dimension     Sex    Age          Unit of measurement  \
4      by selected crime   Total  Total                       Counts   
5      by selected crime   Total  Total                       Counts   
6      by selected crime   Total  Total                       Counts   
7      by selected crime   Total  Total                       Counts   
8      by selected crime   Total  Total                       Counts   
...                  ...     ...    ...                          ...   
84017              Total    Male  Total  Rate per 100,000 population   
84018              Total  Female  Total  Rate per 100,000 population   
84019              Total    Male  Total  Rate per 100,000 population   
84020              Total  Female  Total  Rate per 100,000 population   
84021              Total    Male  Total  Rate per 100,000 population   

             VALUE Source  
4      1443.000000    CTS  
5       417.000000    CTS  
6         2.000000    CTS  
7      9869.000000    CTS  
8        39.000000    CTS  
...            ...    ...  
84017   255.474868    CTS  
84018     4.524110    CTS  
84019   260.183157    CTS  
84020     5.197952    CTS  
84021   296.818546    CTS  

[84018 rows x 9 columns]
df_sorted = df.sort_index(axis=1)
print(df_sorted.truncate(before='Region', after='Sex', axis=1))
               Region     Sex
0      origin changed   Total
1      origin changed   Total
2      origin changed   Total
3      origin changed   Total
4      origin changed   Total
...               ...     ...
84020  origin changed  Female
84021  origin changed    Male
84022  origin changed  Female
84023  origin changed    Male
84024  origin changed  Female

[84025 rows x 2 columns]
short_df = df_sorted.truncate(after=4, axis=0)
short_df = short_df.truncate(before='Region', after='Sex', axis=1)
print(short_df)
           Region    Sex
0  origin changed  Total
1  origin changed  Total
2  origin changed  Total
3  origin changed  Total
4  origin changed  Total
short_df.insert(1, 'New', ['hello', 'hello', 'hello', 'dd', 'dd'],allow_duplicates=True)
print(short_df.drop_duplicates())
           Region    New    Sex
0  origin changed  hello  Total
3  origin changed     dd  Total
df2 = pd.DataFrame({'Region': ['Hawai', 'Costarica'],
                    'Country': ['Country1', 'Country2']
                   })
result = pd.concat([short_df, df2])
print(result)
           Region    New    Sex   Country
0  origin changed  hello  Total       NaN
1  origin changed  hello  Total       NaN
2  origin changed  hello  Total       NaN
3  origin changed     dd  Total       NaN
4  origin changed     dd  Total       NaN
0           Hawai    NaN    NaN  Country1
1       Costarica    NaN    NaN  Country2
print(pd.concat([short_df, df2], ignore_index=True))
           Region    New    Sex   Country
0  origin changed  hello  Total       NaN
1  origin changed  hello  Total       NaN
2  origin changed  hello  Total       NaN
3  origin changed     dd  Total       NaN
4  origin changed     dd  Total       NaN
5           Hawai    NaN    NaN  Country1
6       Costarica    NaN    NaN  Country2
print(pd.concat([short_df, df2], keys=['k1', 'k2']))
              Region    New    Sex   Country
k1 0  origin changed  hello  Total       NaN
   1  origin changed  hello  Total       NaN
   2  origin changed  hello  Total       NaN
   3  origin changed     dd  Total       NaN
   4  origin changed     dd  Total       NaN
k2 0           Hawai    NaN    NaN  Country1
   1       Costarica    NaN    NaN  Country2
print(pd.concat([short_df, df2], keys=['k1', 'k2'], names=['name1', 'name2']))
                     Region    New    Sex   Country
name1 name2                                        
k1    0      origin changed  hello  Total       NaN
      1      origin changed  hello  Total       NaN
      2      origin changed  hello  Total       NaN
      3      origin changed     dd  Total       NaN
      4      origin changed     dd  Total       NaN
k2    0               Hawai    NaN    NaN  Country1
      1           Costarica    NaN    NaN  Country2
print(pd.concat([short_df, df2], join='inner'))
           Region
0  origin changed
1  origin changed
2  origin changed
3  origin changed
4  origin changed
0           Hawai
1       Costarica
print(pd.concat([short_df, df2], axis=1))
           Region    New    Sex     Region   Country
0  origin changed  hello  Total      Hawai  Country1
1  origin changed  hello  Total  Costarica  Country2
2  origin changed  hello  Total        NaN       NaN
3  origin changed     dd  Total        NaN       NaN
4  origin changed     dd  Total        NaN       NaN
print(pd.concat([short_df, df2], verify_integrity=True))
---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)

Cell In[138], line 1
----> 1 print(pd.concat([short_df, df2], verify_integrity=True))


File ~\AppData\Local\Programs\Python\Python314\Lib\site-packages\pandas\core\reshape\concat.py:395, in concat(objs, axis, join, ignore_index, keys, levels, names, verify_integrity, sort, copy)
    380     copy = False
    382 op = _Concatenator(
    383     objs,
    384     axis=axis,
   (...)    392     sort=sort,
    393 )
--> 395 return op.get_result()


File ~\AppData\Local\Programs\Python\Python314\Lib\site-packages\pandas\core\reshape\concat.py:671, in _Concatenator.get_result(self)
    669 for obj in self.objs:
    670     indexers = {}
--> 671     for ax, new_labels in enumerate(self.new_axes):
    672         # ::-1 to convert BlockManager ax to DataFrame ax
    673         if ax == self.bm_axis:
    674             # Suppress reindexing on concat axis
    675             continue


File pandas/_libs/properties.pyx:36, in pandas._libs.properties.CachedProperty.__get__()


File ~\AppData\Local\Programs\Python\Python314\Lib\site-packages\pandas\core\reshape\concat.py:703, in _Concatenator.new_axes(self)
    699 @cache_readonly
    700 def new_axes(self) -> list[Index]:
    701     ndim = self._get_result_dim()
    702     return [
--> 703         self._get_concat_axis if i == self.bm_axis else self._get_comb_axis(i)
    704         for i in range(ndim)
    705     ]


File pandas/_libs/properties.pyx:36, in pandas._libs.properties.CachedProperty.__get__()


File ~\AppData\Local\Programs\Python\Python314\Lib\site-packages\pandas\core\reshape\concat.py:766, in _Concatenator._get_concat_axis(self)
    761 else:
    762     concat_axis = _make_concat_multiindex(
    763         indexes, self.keys, self.levels, self.names
    764     )
--> 766 self._maybe_check_integrity(concat_axis)
    768 return concat_axis


File ~\AppData\Local\Programs\Python\Python314\Lib\site-packages\pandas\core\reshape\concat.py:774, in _Concatenator._maybe_check_integrity(self, concat_index)
    772 if not concat_index.is_unique:
    773     overlap = concat_index[concat_index.duplicated()].unique()
--> 774     raise ValueError(f"Indexes have overlapping values: {overlap}")


ValueError: Indexes have overlapping values: Index([0, 1], dtype='int64')
new_row = pd.Series({"ding": "dong", "dang": "dong"})
print(pd.concat([result, new_row.to_frame().T], ignore_index=True))
           Region    New    Sex   Country  ding  dang
0  origin changed  hello  Total       NaN   NaN   NaN
1  origin changed  hello  Total       NaN   NaN   NaN
2  origin changed  hello  Total       NaN   NaN   NaN
3  origin changed     dd  Total       NaN   NaN   NaN
4  origin changed     dd  Total       NaN   NaN   NaN
5           Hawai    NaN    NaN  Country1   NaN   NaN
6       Costarica    NaN    NaN  Country2   NaN   NaN
7             NaN    NaN    NaN       NaN  dong  dong
profile
오랜시간 망설였던 코딩을 다시 해보려고 노력하고 있는 사람

0개의 댓글