## Some useful Pandas methods for df.index and df.columns
```python
pip install a-pandas-ex-columns-and-index
```
```python
from a_pandas_ex_columns_and_index import pd_add_index_and_columns
pd_add_index_and_columns()
import pandas as pd
df = pd.read_csv("https://github.com/pandas-dev/pandas/raw/main/doc/data/air_quality_long.csv")
```
**The code above will add some new methods to your df**
- df.d_swap_2_columns
- df.ds_sort_by_str_length
- df.d_insert_column_before_another
- df.ds_reverse
- df.d_add_prefix_to_column_when_regex_match
- df.d_add_prefix_to_index_when_regex_match
- df.d_filter_df_by_regex_in_index
- df.d_filter_df_by_regex_in_columns
- df.d_columns_upper
- df.d_index_upper
- df.d_index_lower
- df.d_columns_lower
- df.d_make_columns_dot_compatible
- df.d_natsort_index
- df.d_natort_columns
- df.d_natsort_df_by_column
- d_rename_index
- d_rename_columns
**All methods added to pandas have one of this prefixes:**
- **ds_** (for DataFrames and Series)
- **s_** (only for Series)
- **d_** (only for DataFrames)
### df.d_swap_2_columns
```python
df.columns
Out[3]:
Index(['city', 'country', 'date.utc', 'location', 'parameter', 'value',
'unit'],
dtype='object')
print(df)
city country date.utc ... parameter value unit
0 Antwerpen BE 2019-06-18 06:00:00+00:00 ... pm25 18.0 µg/m³
1 Antwerpen BE 2019-06-17 08:00:00+00:00 ... pm25 6.5 µg/m³
2 Antwerpen BE 2019-06-17 07:00:00+00:00 ... pm25 18.5 µg/m³
3 Antwerpen BE 2019-06-17 06:00:00+00:00 ... pm25 16.0 µg/m³
4 Antwerpen BE 2019-06-17 05:00:00+00:00 ... pm25 7.5 µg/m³
... ... ... ... ... ... ...
5267 London GB 2019-04-09 06:00:00+00:00 ... no2 41.0 µg/m³
5268 London GB 2019-04-09 05:00:00+00:00 ... no2 41.0 µg/m³
5269 London GB 2019-04-09 04:00:00+00:00 ... no2 41.0 µg/m³
5270 London GB 2019-04-09 03:00:00+00:00 ... no2 67.0 µg/m³
5271 London GB 2019-04-09 02:00:00+00:00 ... no2 67.0 µg/m³
[5272 rows x 7 columns]
df2=df.d_swap_2_columns('city', 'country')
print(df2.columns)
print(df2)
Index(['country', 'city', 'date.utc', 'location', 'parameter', 'value',
'unit'],
dtype='object')
country city date.utc ... parameter value unit
0 BE Antwerpen 2019-06-18 06:00:00+00:00 ... pm25 18.0 µg/m³
1 BE Antwerpen 2019-06-17 08:00:00+00:00 ... pm25 6.5 µg/m³
2 BE Antwerpen 2019-06-17 07:00:00+00:00 ... pm25 18.5 µg/m³
3 BE Antwerpen 2019-06-17 06:00:00+00:00 ... pm25 16.0 µg/m³
4 BE Antwerpen 2019-06-17 05:00:00+00:00 ... pm25 7.5 µg/m³
... ... ... ... ... ... ...
5267 GB London 2019-04-09 06:00:00+00:00 ... no2 41.0 µg/m³
5268 GB London 2019-04-09 05:00:00+00:00 ... no2 41.0 µg/m³
5269 GB London 2019-04-09 04:00:00+00:00 ... no2 41.0 µg/m³
5270 GB London 2019-04-09 03:00:00+00:00 ... no2 67.0 µg/m³
5271 GB London 2019-04-09 02:00:00+00:00 ... no2 67.0 µg/m³
[5272 rows x 7 columns]
```
### df.ds_sort_by_str_length
```python
df
Out[3]:
city country date.utc ... parameter value unit
0 Antwerpen BE 2019-06-18 06:00:00+00:00 ... pm25 18.0 µg/m³
1 Antwerpen BE 2019-06-17 08:00:00+00:00 ... pm25 6.5 µg/m³
2 Antwerpen BE 2019-06-17 07:00:00+00:00 ... pm25 18.5 µg/m³
3 Antwerpen BE 2019-06-17 06:00:00+00:00 ... pm25 16.0 µg/m³
4 Antwerpen BE 2019-06-17 05:00:00+00:00 ... pm25 7.5 µg/m³
... ... ... ... ... ... ...
5267 London GB 2019-04-09 06:00:00+00:00 ... no2 41.0 µg/m³
5268 London GB 2019-04-09 05:00:00+00:00 ... no2 41.0 µg/m³
5269 London GB 2019-04-09 04:00:00+00:00 ... no2 41.0 µg/m³
5270 London GB 2019-04-09 03:00:00+00:00 ... no2 67.0 µg/m³
5271 London GB 2019-04-09 02:00:00+00:00 ... no2 67.0 µg/m³
[5272 rows x 7 columns]
df2=df.ds_sort_by_str_length('city')
print(df2)
city country date.utc ... parameter value unit
2635 Paris FR 2019-05-15 05:00:00+00:00 ... no2 46.5 µg/m³
2182 Paris FR 2019-06-03 09:00:00+00:00 ... no2 46.0 µg/m³
2183 Paris FR 2019-06-03 08:00:00+00:00 ... no2 43.9 µg/m³
2184 Paris FR 2019-06-03 07:00:00+00:00 ... no2 50.0 µg/m³
2185 Paris FR 2019-06-03 06:00:00+00:00 ... no2 44.1 µg/m³
... ... ... ... ... ... ...
3554 Antwerpen BE 2019-05-19 15:00:00+00:00 ... no2 33.0 µg/m³
3555 Antwerpen BE 2019-05-19 14:00:00+00:00 ... no2 23.0 µg/m³
3556 Antwerpen BE 2019-05-19 13:00:00+00:00 ... no2 14.5 µg/m³
3548 Antwerpen BE 2019-05-19 21:00:00+00:00 ... no2 12.5 µg/m³
0 Antwerpen BE 2019-06-18 06:00:00+00:00 ... pm25 18.0 µg/m³
[5272 rows x 7 columns]
```
### d_insert_column_before_another
```python
df
Out[6]:
city country date.utc ... parameter value unit
0 Antwerpen BE 2019-06-18 06:00:00+00:00 ... pm25 18.0 µg/m³
1 Antwerpen BE 2019-06-17 08:00:00+00:00 ... pm25 6.5 µg/m³
2 Antwerpen BE 2019-06-17 07:00:00+00:00 ... pm25 18.5 µg/m³
3 Antwerpen BE 2019-06-17 06:00:00+00:00 ... pm25 16.0 µg/m³
4 Antwerpen BE 2019-06-17 05:00:00+00:00 ... pm25 7.5 µg/m³
... ... ... ... ... ... ...
5267 London GB 2019-04-09 06:00:00+00:00 ... no2 41.0 µg/m³
5268 London GB 2019-04-09 05:00:00+00:00 ... no2 41.0 µg/m³
5269 London GB 2019-04-09 04:00:00+00:00 ... no2 41.0 µg/m³
5270 London GB 2019-04-09 03:00:00+00:00 ... no2 67.0 µg/m³
5271 London GB 2019-04-09 02:00:00+00:00 ... no2 67.0 µg/m³
[5272 rows x 7 columns]
df.d_insert_column_before_another(df.city + df.country, 'city_country', 'value')
Out[7]:
city country date.utc ... city_country value unit
0 Antwerpen BE 2019-06-18 06:00:00+00:00 ... AntwerpenBE 18.0 µg/m³
1 Antwerpen BE 2019-06-17 08:00:00+00:00 ... AntwerpenBE 6.5 µg/m³
2 Antwerpen BE 2019-06-17 07:00:00+00:00 ... AntwerpenBE 18.5 µg/m³
3 Antwerpen BE 2019-06-17 06:00:00+00:00 ... AntwerpenBE 16.0 µg/m³
4 Antwerpen BE 2019-06-17 05:00:00+00:00 ... AntwerpenBE 7.5 µg/m³
... ... ... ... ... ... ...
5267 London GB 2019-04-09 06:00:00+00:00 ... LondonGB 41.0 µg/m³
5268 London GB 2019-04-09 05:00:00+00:00 ... LondonGB 41.0 µg/m³
5269 London GB 2019-04-09 04:00:00+00:00 ... LondonGB 41.0 µg/m³
5270 London GB 2019-04-09 03:00:00+00:00 ... LondonGB 67.0 µg/m³
5271 London GB 2019-04-09 02:00:00+00:00 ... LondonGB 67.0 µg/m³
[5272 rows x 8 columns]
```
### df.ds_reverse
```python
df
Out[3]:
city country date.utc ... parameter value unit
0 Antwerpen BE 2019-06-18 06:00:00+00:00 ... pm25 18.0 µg/m³
1 Antwerpen BE 2019-06-17 08:00:00+00:00 ... pm25 6.5 µg/m³
2 Antwerpen BE 2019-06-17 07:00:00+00:00 ... pm25 18.5 µg/m³
3 Antwerpen BE 2019-06-17 06:00:00+00:00 ... pm25 16.0 µg/m³
4 Antwerpen BE 2019-06-17 05:00:00+00:00 ... pm25 7.5 µg/m³
... ... ... ... ... ... ...
5267 London GB 2019-04-09 06:00:00+00:00 ... no2 41.0 µg/m³
5268 London GB 2019-04-09 05:00:00+00:00 ... no2 41.0 µg/m³
5269 London GB 2019-04-09 04:00:00+00:00 ... no2 41.0 µg/m³
5270 London GB 2019-04-09 03:00:00+00:00 ... no2 67.0 µg/m³
5271 London GB 2019-04-09 02:00:00+00:00 ... no2 67.0 µg/m³
[5272 rows x 7 columns]
df.ds_reverse()
Out[4]:
city country date.utc ... parameter value unit
5271 London GB 2019-04-09 02:00:00+00:00 ... no2 67.0 µg/m³
5270 London GB 2019-04-09 03:00:00+00:00 ... no2 67.0 µg/m³
5269 London GB 2019-04-09 04:00:00+00:00 ... no2 41.0 µg/m³
5268 London GB 2019-04-09 05:00:00+00:00 ... no2 41.0 µg/m³
5267 London GB 2019-04-09 06:00:00+00:00 ... no2 41.0 µg/m³
... ... ... ... ... ... ...
4 Antwerpen BE 2019-06-17 05:00:00+00:00 ... pm25 7.5 µg/m³
3 Antwerpen BE 2019-06-17 06:00:00+00:00 ... pm25 16.0 µg/m³
2 Antwerpen BE 2019-06-17 07:00:00+00:00 ... pm25 18.5 µg/m³
1 Antwerpen BE 2019-06-17 08:00:00+00:00 ... pm25 6.5 µg/m³
0 Antwerpen BE 2019-06-18 06:00:00+00:00 ... pm25 18.0 µg/m³
[5272 rows x 7 columns]
```
### df.d_add_prefix_to_column_when_regex_match
```python
df
Out[8]:
city country date.utc ... parameter value unit
0 Antwerpen BE 2019-06-18 06:00:00+00:00 ... pm25 18.0 µg/m³
1 Antwerpen BE 2019-06-17 08:00:00+00:00 ... pm25 6.5 µg/m³
2 Antwerpen BE 2019-06-17 07:00:00+00:00 ... pm25 18.5 µg/m³
3 Antwerpen BE 2019-06-17 06:00:00+00:00 ... pm25 16.0 µg/m³
4 Antwerpen BE 2019-06-17 05:00:00+00:00 ... pm25 7.5 µg/m³
... ... ... ... ... ... ...
5267 London GB 2019-04-09 06:00:00+00:00 ... no2 41.0 µg/m³
5268 London GB 2019-04-09 05:00:00+00:00 ... no2 41.0 µg/m³
5269 London GB 2019-04-09 04:00:00+00:00 ... no2 41.0 µg/m³
5270 London GB 2019-04-09 03:00:00+00:00 ... no2 67.0 µg/m³
5271 London GB 2019-04-09 02:00:00+00:00 ... no2 67.0 µg/m³
[5272 rows x 7 columns]
df.d_add_prefix_to_column_when_regex_match(prefix='aa_', regular_expression='^c')
Out[9]:
aa_city aa_country date.utc ... parameter value unit
0 Antwerpen BE 2019-06-18 06:00:00+00:00 ... pm25 18.0 µg/m³
1 Antwerpen BE 2019-06-17 08:00:00+00:00 ... pm25 6.5 µg/m³
2 Antwerpen BE 2019-06-17 07:00:00+00:00 ... pm25 18.5 µg/m³
3 Antwerpen BE 2019-06-17 06:00:00+00:00 ... pm25 16.0 µg/m³
4 Antwerpen BE 2019-06-17 05:00:00+00:00 ... pm25 7.5 µg/m³
... ... ... ... ... ... ...
5267 London GB 2019-04-09 06:00:00+00:00 ... no2 41.0 µg/m³
5268 London GB 2019-04-09 05:00:00+00:00 ... no2 41.0 µg/m³
5269 London GB 2019-04-09 04:00:00+00:00 ... no2 41.0 µg/m³
5270 London GB 2019-04-09 03:00:00+00:00 ... no2 67.0 µg/m³
5271 London GB 2019-04-09 02:00:00+00:00 ... no2 67.0 µg/m³
[5272 rows x 7 columns]
```
### df.d_add_prefix_to_index_when_regex_match
```python
Out[12]:
city country date.utc ... parameter value unit
0 Antwerpen BE 2019-06-18 06:00:00+00:00 ... pm25 18.0 µg/m³
1 Antwerpen BE 2019-06-17 08:00:00+00:00 ... pm25 6.5 µg/m³
2 Antwerpen BE 2019-06-17 07:00:00+00:00 ... pm25 18.5 µg/m³
3 Antwerpen BE 2019-06-17 06:00:00+00:00 ... pm25 16.0 µg/m³
4 Antwerpen BE 2019-06-17 05:00:00+00:00 ... pm25 7.5 µg/m³
... ... ... ... ... ... ...
5267 London GB 2019-04-09 06:00:00+00:00 ... no2 41.0 µg/m³
5268 London GB 2019-04-09 05:00:00+00:00 ... no2 41.0 µg/m³
5269 London GB 2019-04-09 04:00:00+00:00 ... no2 41.0 µg/m³
5270 London GB 2019-04-09 03:00:00+00:00 ... no2 67.0 µg/m³
5271 London GB 2019-04-09 02:00:00+00:00 ... no2 67.0 µg/m³
[5272 rows x 7 columns]
df.d_add_prefix_to_index_when_regex_match('five_', regular_expression='^5')
Out[13]:
city country date.utc ... parameter value unit
0 Antwerpen BE 2019-06-18 06:00:00+00:00 ... pm25 18.0 µg/m³
1 Antwerpen BE 2019-06-17 08:00:00+00:00 ... pm25 6.5 µg/m³
2 Antwerpen BE 2019-06-17 07:00:00+00:00 ... pm25 18.5 µg/m³
3 Antwerpen BE 2019-06-17 06:00:00+00:00 ... pm25 16.0 µg/m³
4 Antwerpen BE 2019-06-17 05:00:00+00:00 ... pm25 7.5 µg/m³
... ... ... ... ... ... ...
five_5267 London GB 2019-04-09 06:00:00+00:00 ... no2 41.0 µg/m³
five_5268 London GB 2019-04-09 05:00:00+00:00 ... no2 41.0 µg/m³
five_5269 London GB 2019-04-09 04:00:00+00:00 ... no2 41.0 µg/m³
five_5270 London GB 2019-04-09 03:00:00+00:00 ... no2 67.0 µg/m³
five_5271 London GB 2019-04-09 02:00:00+00:00 ... no2 67.0 µg/m³
[5272 rows x 7 columns]
```
### df.d_filter_df_by_regex_in_columns
```python
df
Out[14]:
city country date.utc ... parameter value unit
0 Antwerpen BE 2019-06-18 06:00:00+00:00 ... pm25 18.0 µg/m³
1 Antwerpen BE 2019-06-17 08:00:00+00:00 ... pm25 6.5 µg/m³
2 Antwerpen BE 2019-06-17 07:00:00+00:00 ... pm25 18.5 µg/m³
3 Antwerpen BE 2019-06-17 06:00:00+00:00 ... pm25 16.0 µg/m³
4 Antwerpen BE 2019-06-17 05:00:00+00:00 ... pm25 7.5 µg/m³
... ... ... ... ... ... ...
5267 London GB 2019-04-09 06:00:00+00:00 ... no2 41.0 µg/m³
5268 London GB 2019-04-09 05:00:00+00:00 ... no2 41.0 µg/m³
5269 London GB 2019-04-09 04:00:00+00:00 ... no2 41.0 µg/m³
5270 London GB 2019-04-09 03:00:00+00:00 ... no2 67.0 µg/m³
5271 London GB 2019-04-09 02:00:00+00:00 ... no2 67.0 µg/m³
[5272 rows x 7 columns]
df.d_filter_df_by_regex_in_columns('^[cu]')
Out[15]:
city country unit
0 Antwerpen BE µg/m³
1 Antwerpen BE µg/m³
2 Antwerpen BE µg/m³
3 Antwerpen BE µg/m³
4 Antwerpen BE µg/m³
... ... ...
5267 London GB µg/m³
5268 London GB µg/m³
5269 London GB µg/m³
5270 London GB µg/m³
5271 London GB µg/m³
[5272 rows x 3 columns]
```
### df.d_filter_df_by_regex_in_index
```python
df
Out[16]:
city country date.utc ... parameter value unit
0 Antwerpen BE 2019-06-18 06:00:00+00:00 ... pm25 18.0 µg/m³
1 Antwerpen BE 2019-06-17 08:00:00+00:00 ... pm25 6.5 µg/m³
2 Antwerpen BE 2019-06-17 07:00:00+00:00 ... pm25 18.5 µg/m³
3 Antwerpen BE 2019-06-17 06:00:00+00:00 ... pm25 16.0 µg/m³
4 Antwerpen BE 2019-06-17 05:00:00+00:00 ... pm25 7.5 µg/m³
... ... ... ... ... ... ...
5267 London GB 2019-04-09 06:00:00+00:00 ... no2 41.0 µg/m³
5268 London GB 2019-04-09 05:00:00+00:00 ... no2 41.0 µg/m³
5269 London GB 2019-04-09 04:00:00+00:00 ... no2 41.0 µg/m³
5270 London GB 2019-04-09 03:00:00+00:00 ... no2 67.0 µg/m³
5271 London GB 2019-04-09 02:00:00+00:00 ... no2 67.0 µg/m³
[5272 rows x 7 columns]
df.d_filter_df_by_regex_in_index(r'^5\d\d0$')
Out[17]:
city country date.utc ... parameter value unit
5000 London GB 2019-04-20 16:00:00+00:00 ... no2 48.0 µg/m³
5010 London GB 2019-04-20 06:00:00+00:00 ... no2 33.0 µg/m³
5020 London GB 2019-04-19 20:00:00+00:00 ... no2 58.0 µg/m³
5030 London GB 2019-04-19 10:00:00+00:00 ... no2 44.0 µg/m³
5040 London GB 2019-04-18 23:00:00+00:00 ... no2 61.0 µg/m³
5050 London GB 2019-04-18 13:00:00+00:00 ... no2 49.0 µg/m³
5060 London GB 2019-04-18 03:00:00+00:00 ... no2 50.0 µg/m³
5070 London GB 2019-04-17 17:00:00+00:00 ... no2 54.0 µg/m³
5080 London GB 2019-04-17 07:00:00+00:00 ... no2 51.0 µg/m³
5090 London GB 2019-04-16 20:00:00+00:00 ... no2 83.0 µg/m³
5100 London GB 2019-04-16 09:00:00+00:00 ... no2 66.0 µg/m³
5110 London GB 2019-04-15 22:00:00+00:00 ... no2 47.0 µg/m³
5120 London GB 2019-04-15 12:00:00+00:00 ... no2 27.0 µg/m³
5130 London GB 2019-04-15 02:00:00+00:00 ... no2 32.0 µg/m³
5140 London GB 2019-04-14 16:00:00+00:00 ... no2 23.0 µg/m³
5150 London GB 2019-04-14 06:00:00+00:00 ... no2 35.0 µg/m³
5160 London GB 2019-04-13 20:00:00+00:00 ... no2 29.0 µg/m³
5170 London GB 2019-04-13 10:00:00+00:00 ... no2 45.0 µg/m³
5180 London GB 2019-04-13 00:00:00+00:00 ... no2 29.0 µg/m³
5190 London GB 2019-04-12 14:00:00+00:00 ... no2 39.0 µg/m³
5200 London GB 2019-04-12 04:00:00+00:00 ... no2 33.0 µg/m³
5210 London GB 2019-04-11 16:00:00+00:00 ... no2 34.0 µg/m³
5220 London GB 2019-04-11 06:00:00+00:00 ... no2 46.0 µg/m³
5230 London GB 2019-04-10 19:00:00+00:00 ... no2 35.0 µg/m³
5240 London GB 2019-04-10 09:00:00+00:00 ... no2 35.0 µg/m³
5250 London GB 2019-04-09 23:00:00+00:00 ... no2 38.0 µg/m³
5260 London GB 2019-04-09 13:00:00+00:00 ... no2 56.0 µg/m³
5270 London GB 2019-04-09 03:00:00+00:00 ... no2 67.0 µg/m³
[28 rows x 7 columns]
```
### df.d_columns_upper, df.d_columns_lower,df.d_make_columns_dot_compatible,df.d_index_upper,df.d_index_lower
```python
print(df.columns)
print(df.d_make_columns_dot_compatible().columns)
print(df.d_columns_upper().columns)
print(df.d_columns_lower().columns)
df2=df.copy()
df2.index = df2.parameter
print(df2.index)
print(df2.d_index_upper().index)
print(df2.d_index_lower().index)
Index(['city', 'country', 'date.utc', 'location', 'parameter', 'value',
'unit'],
dtype='object')
Index(['city', 'country', 'date_utc', 'location', 'parameter', 'value',
'unit'],
dtype='object')
Index(['CITY', 'COUNTRY', 'DATE_UTC', 'LOCATION', 'PARAMETER', 'VALUE',
'UNIT'],
dtype='object')
Index(['city', 'country', 'date_utc', 'location', 'parameter', 'value',
'unit'],
dtype='object')
Index(['pm25', 'pm25', 'pm25', 'pm25', 'pm25', 'pm25', 'pm25', 'pm25', 'pm25',
'pm25',
...
'no2', 'no2', 'no2', 'no2', 'no2', 'no2', 'no2', 'no2', 'no2', 'no2'],
dtype='object', name='parameter', length=5272)
Index(['PM25', 'PM25', 'PM25', 'PM25', 'PM25', 'PM25', 'PM25', 'PM25', 'PM25',
'PM25',
...
'NO2', 'NO2', 'NO2', 'NO2', 'NO2', 'NO2', 'NO2', 'NO2', 'NO2', 'NO2'],
dtype='object', length=5272)
Index(['pm25', 'pm25', 'pm25', 'pm25', 'pm25', 'pm25', 'pm25', 'pm25', 'pm25',
'pm25',
...
'no2', 'no2', 'no2', 'no2', 'no2', 'no2', 'no2', 'no2', 'no2', 'no2'],
dtype='object', length=5272)
```
### df.d_natsort_index
```python
df2 = df.sample(len(df)).copy()
dftempindex = df2.index[:2500].to_list()
tempvalue = df2.loc[dftempindex].parameter.copy()
tempvalue = tempvalue.apply(lambda x: str(x).upper())
df2.loc[dftempindex, 'parameter'] = tempvalue
print(df2)
df2.index = df2.parameter
print(df2.d_natsort_index())
for a,b,c,d in zip(df2.d_natsort_index(sort_numbers_after_non_numbers=True).index.to_list(), df2.d_natsort_index(lowercase_first=True).index.to_list(),df2.d_natsort_index(group_lower_and_uppercase=True).index.to_list(),df2.d_natsort_index(uppercase_first=True).index.to_list()):
print(a,b,c,d)
no2 NO2 no2 no2
no2 NO2 no2 no2
no2 NO2 no2 no2
no2 NO2 no2 no2
no2 NO2 no2 no2
no2 NO2 no2 no2
no2 NO2 no2 no2
no2 NO2 no2 no2
no2 NO2 no2 no2
no2 NO2 no2 no2
no2 NO2 no2 no2
no2 NO2 no2 no2
no2 NO2 no2 no2
no2 NO2 no2 no2
no2 NO2 no2 no2
no2 NO2 no2 no2
no2 NO2 no2 no2
no2 NO2 no2 no2
no2 NO2 no2 no2
no2 NO2 no2 no2
no2 NO2 no2 no2
no2 NO2 no2 no2
no2 NO2 PM25 no2
no2 NO2 PM25 no2
no2 NO2 PM25 no2
no2 NO2 PM25 no2
no2 NO2 PM25 no2
no2 NO2 PM25 no2
no2 NO2 PM25 no2
no2 NO2 PM25 no2
no2 NO2 PM25 no2
no2 NO2 PM25 no2
no2 NO2 PM25 no2
no2 NO2 PM25 no2
no2 NO2 PM25 no2
no2 NO2 PM25 no2
no2 NO2 PM25 no2
no2 NO2 PM25 no2
no2 NO2 PM25 no2
no2 NO2 PM25 no2
no2 NO2 PM25 no2
no2 NO2 PM25 no2
no2 NO2 PM25 no2
no2 NO2 PM25 no2
no2 NO2 PM25 no2
no2 NO2 PM25 no2
no2 NO2 PM25 no2
no2 NO2 PM25 no2
no2 NO2 PM25 no2
```
### df.d_natsort_df_by_column
```python
df
Out[3]:
city country date.utc ... parameter value unit
0 Antwerpen BE 2019-06-18 06:00:00+00:00 ... pm25 18.0 µg/m³
1 Antwerpen BE 2019-06-17 08:00:00+00:00 ... pm25 6.5 µg/m³
2 Antwerpen BE 2019-06-17 07:00:00+00:00 ... pm25 18.5 µg/m³
3 Antwerpen BE 2019-06-17 06:00:00+00:00 ... pm25 16.0 µg/m³
4 Antwerpen BE 2019-06-17 05:00:00+00:00 ... pm25 7.5 µg/m³
... ... ... ... ... ... ...
5267 London GB 2019-04-09 06:00:00+00:00 ... no2 41.0 µg/m³
5268 London GB 2019-04-09 05:00:00+00:00 ... no2 41.0 µg/m³
5269 London GB 2019-04-09 04:00:00+00:00 ... no2 41.0 µg/m³
5270 London GB 2019-04-09 03:00:00+00:00 ... no2 67.0 µg/m³
5271 London GB 2019-04-09 02:00:00+00:00 ... no2 67.0 µg/m³
[5272 rows x 7 columns]
df.d_natsort_df_by_column('date.utc')
Out[4]:
city country date.utc ... parameter value unit
176 Antwerpen BE 2019-04-09 01:00:00+00:00 ... pm25 76.0 µg/m³
3500 Paris FR 2019-04-09 01:00:00+00:00 ... no2 24.4 µg/m³
3663 Antwerpen BE 2019-04-09 01:00:00+00:00 ... no2 22.5 µg/m³
175 Antwerpen BE 2019-04-09 02:00:00+00:00 ... pm25 91.5 µg/m³
1824 London GB 2019-04-09 02:00:00+00:00 ... pm25 42.0 µg/m³
... ... ... ... ... ... ...
1827 Paris FR 2019-06-20 22:00:00+00:00 ... no2 26.5 µg/m³
178 London GB 2019-06-20 23:00:00+00:00 ... pm25 7.0 µg/m³
1826 Paris FR 2019-06-20 23:00:00+00:00 ... no2 21.8 µg/m³
177 London GB 2019-06-21 00:00:00+00:00 ... pm25 7.0 µg/m³
1825 Paris FR 2019-06-21 00:00:00+00:00 ... no2 20.0 µg/m³
[5272 rows x 7 columns]
```
### df.d_rename_columns / df.d_rename_index
```python
df = pd.read_csv( "https://github.com/pandas-dev/pandas/raw/main/doc/data/titanic.csv")
print(df)
PassengerId Survived Pclass ... Fare Cabin Embarked
0 1 0 3 ... 7.2500 NaN S
1 2 1 1 ... 71.2833 C85 C
2 3 1 3 ... 7.9250 NaN S
3 4 1 1 ... 53.1000 C123 S
4 5 0 3 ... 8.0500 NaN S
.. ... ... ... ... ... ... ...
886 887 0 2 ... 13.0000 NaN S
887 888 1 1 ... 30.0000 B42 S
888 889 0 3 ... 23.4500 NaN S
889 890 1 1 ... 30.0000 C148 C
890 891 0 3 ... 7.7500 NaN Q
df.d_rename_columns(Fare='Embarked',Embarked='Fare',Cabin='cabin2')
df.d_rename_index({1: 1000000,2:50022})
print(df)
PassengerId Survived Pclass ... Embarked cabin2 Fare
0 1 0 3 ... 7.2500 NaN S
1000000 2 1 1 ... 71.2833 C85 C
50022 3 1 3 ... 7.9250 NaN S
3 4 1 1 ... 53.1000 C123 S
4 5 0 3 ... 8.0500 NaN S
... ... ... ... ... ... ...
886 887 0 2 ... 13.0000 NaN S
887 888 1 1 ... 30.0000 B42 S
888 889 0 3 ... 23.4500 NaN S
889 890 1 1 ... 30.0000 C148 C
890 891 0 3 ... 7.7500 NaN Q
[891 rows x 12 columns]
df.d_rename_columns({'Embarked': 'Fare', 'Fare' : 'Embarked', 'cabin2' : 'Cabin'})
df.index = df.index.astype('string')
df.index = 'a' + df.index
df.d_rename_index(a1000000= 1,a50022=2)
print(df)
PassengerId Survived Pclass ... Fare Cabin Embarked
a0 1 0 3 ... 7.2500 NaN S
1 2 1 1 ... 71.2833 C85 C
2 3 1 3 ... 7.9250 NaN S
a3 4 1 1 ... 53.1000 C123 S
a4 5 0 3 ... 8.0500 NaN S
... ... ... ... ... ... ...
a886 887 0 2 ... 13.0000 NaN S
a887 888 1 1 ... 30.0000 B42 S
a888 889 0 3 ... 23.4500 NaN S
a889 890 1 1 ... 30.0000 C148 C
a890 891 0 3 ... 7.7500 NaN Q
```
Raw data
{
"_id": null,
"home_page": "https://github.com/hansalemaos/a_pandas_ex_columns_and_index",
"name": "a-pandas-ex-columns-and-index",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "pandas,numpy,dataframe,series,regex,natsort",
"author": "Johannes Fischer",
"author_email": "<aulasparticularesdealemaosp@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/d1/84/b7e02dd5436b2f85a5b2bdfd5695b1e6b2785949aed88475a44ed057fe8d/a_pandas_ex_columns_and_index-0.13.tar.gz",
"platform": null,
"description": "\n## Some useful Pandas methods for df.index and df.columns\n\n\n\n```python\n\npip install a-pandas-ex-columns-and-index \n\n```\n\n\n\n```python\n\nfrom a_pandas_ex_columns_and_index import pd_add_index_and_columns\n\npd_add_index_and_columns()\n\nimport pandas as pd\n\ndf = pd.read_csv(\"https://github.com/pandas-dev/pandas/raw/main/doc/data/air_quality_long.csv\")\n\n```\n\n\n\n**The code above will add some new methods to your df**\n\n\n\n- df.d_swap_2_columns \n\n- df.ds_sort_by_str_length \n\n- df.d_insert_column_before_another \n\n- df.ds_reverse \n\n- df.d_add_prefix_to_column_when_regex_match \n\n- df.d_add_prefix_to_index_when_regex_match \n\n- df.d_filter_df_by_regex_in_index \n\n- df.d_filter_df_by_regex_in_columns \n\n- df.d_columns_upper \n\n- df.d_index_upper \n\n- df.d_index_lower \n\n- df.d_columns_lower \n\n- df.d_make_columns_dot_compatible \n\n- df.d_natsort_index \n\n- df.d_natort_columns \n\n- df.d_natsort_df_by_column \n\n- d_rename_index\n\n- d_rename_columns\n\n\n\n**All methods added to pandas have one of this prefixes:**\n\n\n\n- **ds_**\u00a0(for DataFrames and Series)\n\n\n\n- **s_**\u00a0(only for Series)\n\n\n\n- **d_**\u00a0(only for DataFrames)\n\n\n\n### df.d_swap_2_columns\n\n\n\n```python\n\ndf.columns\n\nOut[3]: \n\nIndex(['city', 'country', 'date.utc', 'location', 'parameter', 'value',\n\n 'unit'],\n\n dtype='object')\n\nprint(df)\n\n city country date.utc ... parameter value unit\n\n0 Antwerpen BE 2019-06-18 06:00:00+00:00 ... pm25 18.0 \u00b5g/m\u00b3\n\n1 Antwerpen BE 2019-06-17 08:00:00+00:00 ... pm25 6.5 \u00b5g/m\u00b3\n\n2 Antwerpen BE 2019-06-17 07:00:00+00:00 ... pm25 18.5 \u00b5g/m\u00b3\n\n3 Antwerpen BE 2019-06-17 06:00:00+00:00 ... pm25 16.0 \u00b5g/m\u00b3\n\n4 Antwerpen BE 2019-06-17 05:00:00+00:00 ... pm25 7.5 \u00b5g/m\u00b3\n\n ... ... ... ... ... ... ...\n\n5267 London GB 2019-04-09 06:00:00+00:00 ... no2 41.0 \u00b5g/m\u00b3\n\n5268 London GB 2019-04-09 05:00:00+00:00 ... no2 41.0 \u00b5g/m\u00b3\n\n5269 London GB 2019-04-09 04:00:00+00:00 ... no2 41.0 \u00b5g/m\u00b3\n\n5270 London GB 2019-04-09 03:00:00+00:00 ... no2 67.0 \u00b5g/m\u00b3\n\n5271 London GB 2019-04-09 02:00:00+00:00 ... no2 67.0 \u00b5g/m\u00b3\n\n[5272 rows x 7 columns]\n\ndf2=df.d_swap_2_columns('city', 'country')\n\nprint(df2.columns)\n\nprint(df2)\n\nIndex(['country', 'city', 'date.utc', 'location', 'parameter', 'value',\n\n 'unit'],\n\n dtype='object')\n\n country city date.utc ... parameter value unit\n\n0 BE Antwerpen 2019-06-18 06:00:00+00:00 ... pm25 18.0 \u00b5g/m\u00b3\n\n1 BE Antwerpen 2019-06-17 08:00:00+00:00 ... pm25 6.5 \u00b5g/m\u00b3\n\n2 BE Antwerpen 2019-06-17 07:00:00+00:00 ... pm25 18.5 \u00b5g/m\u00b3\n\n3 BE Antwerpen 2019-06-17 06:00:00+00:00 ... pm25 16.0 \u00b5g/m\u00b3\n\n4 BE Antwerpen 2019-06-17 05:00:00+00:00 ... pm25 7.5 \u00b5g/m\u00b3\n\n ... ... ... ... ... ... ...\n\n5267 GB London 2019-04-09 06:00:00+00:00 ... no2 41.0 \u00b5g/m\u00b3\n\n5268 GB London 2019-04-09 05:00:00+00:00 ... no2 41.0 \u00b5g/m\u00b3\n\n5269 GB London 2019-04-09 04:00:00+00:00 ... no2 41.0 \u00b5g/m\u00b3\n\n5270 GB London 2019-04-09 03:00:00+00:00 ... no2 67.0 \u00b5g/m\u00b3\n\n5271 GB London 2019-04-09 02:00:00+00:00 ... no2 67.0 \u00b5g/m\u00b3\n\n[5272 rows x 7 columns]\n\n```\n\n\n\n### df.ds_sort_by_str_length\n\n\n\n```python\n\ndf\n\nOut[3]: \n\n city country date.utc ... parameter value unit\n\n0 Antwerpen BE 2019-06-18 06:00:00+00:00 ... pm25 18.0 \u00b5g/m\u00b3\n\n1 Antwerpen BE 2019-06-17 08:00:00+00:00 ... pm25 6.5 \u00b5g/m\u00b3\n\n2 Antwerpen BE 2019-06-17 07:00:00+00:00 ... pm25 18.5 \u00b5g/m\u00b3\n\n3 Antwerpen BE 2019-06-17 06:00:00+00:00 ... pm25 16.0 \u00b5g/m\u00b3\n\n4 Antwerpen BE 2019-06-17 05:00:00+00:00 ... pm25 7.5 \u00b5g/m\u00b3\n\n ... ... ... ... ... ... ...\n\n5267 London GB 2019-04-09 06:00:00+00:00 ... no2 41.0 \u00b5g/m\u00b3\n\n5268 London GB 2019-04-09 05:00:00+00:00 ... no2 41.0 \u00b5g/m\u00b3\n\n5269 London GB 2019-04-09 04:00:00+00:00 ... no2 41.0 \u00b5g/m\u00b3\n\n5270 London GB 2019-04-09 03:00:00+00:00 ... no2 67.0 \u00b5g/m\u00b3\n\n5271 London GB 2019-04-09 02:00:00+00:00 ... no2 67.0 \u00b5g/m\u00b3\n\n[5272 rows x 7 columns]\n\ndf2=df.ds_sort_by_str_length('city')\n\nprint(df2)\n\n city country date.utc ... parameter value unit\n\n2635 Paris FR 2019-05-15 05:00:00+00:00 ... no2 46.5 \u00b5g/m\u00b3\n\n2182 Paris FR 2019-06-03 09:00:00+00:00 ... no2 46.0 \u00b5g/m\u00b3\n\n2183 Paris FR 2019-06-03 08:00:00+00:00 ... no2 43.9 \u00b5g/m\u00b3\n\n2184 Paris FR 2019-06-03 07:00:00+00:00 ... no2 50.0 \u00b5g/m\u00b3\n\n2185 Paris FR 2019-06-03 06:00:00+00:00 ... no2 44.1 \u00b5g/m\u00b3\n\n ... ... ... ... ... ... ...\n\n3554 Antwerpen BE 2019-05-19 15:00:00+00:00 ... no2 33.0 \u00b5g/m\u00b3\n\n3555 Antwerpen BE 2019-05-19 14:00:00+00:00 ... no2 23.0 \u00b5g/m\u00b3\n\n3556 Antwerpen BE 2019-05-19 13:00:00+00:00 ... no2 14.5 \u00b5g/m\u00b3\n\n3548 Antwerpen BE 2019-05-19 21:00:00+00:00 ... no2 12.5 \u00b5g/m\u00b3\n\n0 Antwerpen BE 2019-06-18 06:00:00+00:00 ... pm25 18.0 \u00b5g/m\u00b3\n\n[5272 rows x 7 columns]\n\n```\n\n\n\n### d_insert_column_before_another\n\n\n\n```python\n\ndf\n\nOut[6]: \n\n city country date.utc ... parameter value unit\n\n0 Antwerpen BE 2019-06-18 06:00:00+00:00 ... pm25 18.0 \u00b5g/m\u00b3\n\n1 Antwerpen BE 2019-06-17 08:00:00+00:00 ... pm25 6.5 \u00b5g/m\u00b3\n\n2 Antwerpen BE 2019-06-17 07:00:00+00:00 ... pm25 18.5 \u00b5g/m\u00b3\n\n3 Antwerpen BE 2019-06-17 06:00:00+00:00 ... pm25 16.0 \u00b5g/m\u00b3\n\n4 Antwerpen BE 2019-06-17 05:00:00+00:00 ... pm25 7.5 \u00b5g/m\u00b3\n\n ... ... ... ... ... ... ...\n\n5267 London GB 2019-04-09 06:00:00+00:00 ... no2 41.0 \u00b5g/m\u00b3\n\n5268 London GB 2019-04-09 05:00:00+00:00 ... no2 41.0 \u00b5g/m\u00b3\n\n5269 London GB 2019-04-09 04:00:00+00:00 ... no2 41.0 \u00b5g/m\u00b3\n\n5270 London GB 2019-04-09 03:00:00+00:00 ... no2 67.0 \u00b5g/m\u00b3\n\n5271 London GB 2019-04-09 02:00:00+00:00 ... no2 67.0 \u00b5g/m\u00b3\n\n[5272 rows x 7 columns]\n\ndf.d_insert_column_before_another(df.city + df.country, 'city_country', 'value')\n\nOut[7]: \n\n city country date.utc ... city_country value unit\n\n0 Antwerpen BE 2019-06-18 06:00:00+00:00 ... AntwerpenBE 18.0 \u00b5g/m\u00b3\n\n1 Antwerpen BE 2019-06-17 08:00:00+00:00 ... AntwerpenBE 6.5 \u00b5g/m\u00b3\n\n2 Antwerpen BE 2019-06-17 07:00:00+00:00 ... AntwerpenBE 18.5 \u00b5g/m\u00b3\n\n3 Antwerpen BE 2019-06-17 06:00:00+00:00 ... AntwerpenBE 16.0 \u00b5g/m\u00b3\n\n4 Antwerpen BE 2019-06-17 05:00:00+00:00 ... AntwerpenBE 7.5 \u00b5g/m\u00b3\n\n ... ... ... ... ... ... ...\n\n5267 London GB 2019-04-09 06:00:00+00:00 ... LondonGB 41.0 \u00b5g/m\u00b3\n\n5268 London GB 2019-04-09 05:00:00+00:00 ... LondonGB 41.0 \u00b5g/m\u00b3\n\n5269 London GB 2019-04-09 04:00:00+00:00 ... LondonGB 41.0 \u00b5g/m\u00b3\n\n5270 London GB 2019-04-09 03:00:00+00:00 ... LondonGB 67.0 \u00b5g/m\u00b3\n\n5271 London GB 2019-04-09 02:00:00+00:00 ... LondonGB 67.0 \u00b5g/m\u00b3\n\n[5272 rows x 8 columns]\n\n```\n\n\n\n### df.ds_reverse\n\n\n\n```python\n\ndf\n\nOut[3]: \n\n city country date.utc ... parameter value unit\n\n0 Antwerpen BE 2019-06-18 06:00:00+00:00 ... pm25 18.0 \u00b5g/m\u00b3\n\n1 Antwerpen BE 2019-06-17 08:00:00+00:00 ... pm25 6.5 \u00b5g/m\u00b3\n\n2 Antwerpen BE 2019-06-17 07:00:00+00:00 ... pm25 18.5 \u00b5g/m\u00b3\n\n3 Antwerpen BE 2019-06-17 06:00:00+00:00 ... pm25 16.0 \u00b5g/m\u00b3\n\n4 Antwerpen BE 2019-06-17 05:00:00+00:00 ... pm25 7.5 \u00b5g/m\u00b3\n\n ... ... ... ... ... ... ...\n\n5267 London GB 2019-04-09 06:00:00+00:00 ... no2 41.0 \u00b5g/m\u00b3\n\n5268 London GB 2019-04-09 05:00:00+00:00 ... no2 41.0 \u00b5g/m\u00b3\n\n5269 London GB 2019-04-09 04:00:00+00:00 ... no2 41.0 \u00b5g/m\u00b3\n\n5270 London GB 2019-04-09 03:00:00+00:00 ... no2 67.0 \u00b5g/m\u00b3\n\n5271 London GB 2019-04-09 02:00:00+00:00 ... no2 67.0 \u00b5g/m\u00b3\n\n[5272 rows x 7 columns]\n\ndf.ds_reverse()\n\nOut[4]: \n\n city country date.utc ... parameter value unit\n\n5271 London GB 2019-04-09 02:00:00+00:00 ... no2 67.0 \u00b5g/m\u00b3\n\n5270 London GB 2019-04-09 03:00:00+00:00 ... no2 67.0 \u00b5g/m\u00b3\n\n5269 London GB 2019-04-09 04:00:00+00:00 ... no2 41.0 \u00b5g/m\u00b3\n\n5268 London GB 2019-04-09 05:00:00+00:00 ... no2 41.0 \u00b5g/m\u00b3\n\n5267 London GB 2019-04-09 06:00:00+00:00 ... no2 41.0 \u00b5g/m\u00b3\n\n ... ... ... ... ... ... ...\n\n4 Antwerpen BE 2019-06-17 05:00:00+00:00 ... pm25 7.5 \u00b5g/m\u00b3\n\n3 Antwerpen BE 2019-06-17 06:00:00+00:00 ... pm25 16.0 \u00b5g/m\u00b3\n\n2 Antwerpen BE 2019-06-17 07:00:00+00:00 ... pm25 18.5 \u00b5g/m\u00b3\n\n1 Antwerpen BE 2019-06-17 08:00:00+00:00 ... pm25 6.5 \u00b5g/m\u00b3\n\n0 Antwerpen BE 2019-06-18 06:00:00+00:00 ... pm25 18.0 \u00b5g/m\u00b3\n\n[5272 rows x 7 columns]\n\n```\n\n\n\n### df.d_add_prefix_to_column_when_regex_match\n\n\n\n```python\n\ndf\n\nOut[8]: \n\n city country date.utc ... parameter value unit\n\n0 Antwerpen BE 2019-06-18 06:00:00+00:00 ... pm25 18.0 \u00b5g/m\u00b3\n\n1 Antwerpen BE 2019-06-17 08:00:00+00:00 ... pm25 6.5 \u00b5g/m\u00b3\n\n2 Antwerpen BE 2019-06-17 07:00:00+00:00 ... pm25 18.5 \u00b5g/m\u00b3\n\n3 Antwerpen BE 2019-06-17 06:00:00+00:00 ... pm25 16.0 \u00b5g/m\u00b3\n\n4 Antwerpen BE 2019-06-17 05:00:00+00:00 ... pm25 7.5 \u00b5g/m\u00b3\n\n ... ... ... ... ... ... ...\n\n5267 London GB 2019-04-09 06:00:00+00:00 ... no2 41.0 \u00b5g/m\u00b3\n\n5268 London GB 2019-04-09 05:00:00+00:00 ... no2 41.0 \u00b5g/m\u00b3\n\n5269 London GB 2019-04-09 04:00:00+00:00 ... no2 41.0 \u00b5g/m\u00b3\n\n5270 London GB 2019-04-09 03:00:00+00:00 ... no2 67.0 \u00b5g/m\u00b3\n\n5271 London GB 2019-04-09 02:00:00+00:00 ... no2 67.0 \u00b5g/m\u00b3\n\n[5272 rows x 7 columns]\n\ndf.d_add_prefix_to_column_when_regex_match(prefix='aa_', regular_expression='^c')\n\nOut[9]: \n\n aa_city aa_country date.utc ... parameter value unit\n\n0 Antwerpen BE 2019-06-18 06:00:00+00:00 ... pm25 18.0 \u00b5g/m\u00b3\n\n1 Antwerpen BE 2019-06-17 08:00:00+00:00 ... pm25 6.5 \u00b5g/m\u00b3\n\n2 Antwerpen BE 2019-06-17 07:00:00+00:00 ... pm25 18.5 \u00b5g/m\u00b3\n\n3 Antwerpen BE 2019-06-17 06:00:00+00:00 ... pm25 16.0 \u00b5g/m\u00b3\n\n4 Antwerpen BE 2019-06-17 05:00:00+00:00 ... pm25 7.5 \u00b5g/m\u00b3\n\n ... ... ... ... ... ... ...\n\n5267 London GB 2019-04-09 06:00:00+00:00 ... no2 41.0 \u00b5g/m\u00b3\n\n5268 London GB 2019-04-09 05:00:00+00:00 ... no2 41.0 \u00b5g/m\u00b3\n\n5269 London GB 2019-04-09 04:00:00+00:00 ... no2 41.0 \u00b5g/m\u00b3\n\n5270 London GB 2019-04-09 03:00:00+00:00 ... no2 67.0 \u00b5g/m\u00b3\n\n5271 London GB 2019-04-09 02:00:00+00:00 ... no2 67.0 \u00b5g/m\u00b3\n\n[5272 rows x 7 columns]\n\n```\n\n\n\n### df.d_add_prefix_to_index_when_regex_match\n\n\n\n```python\n\nOut[12]: \n\n city country date.utc ... parameter value unit\n\n0 Antwerpen BE 2019-06-18 06:00:00+00:00 ... pm25 18.0 \u00b5g/m\u00b3\n\n1 Antwerpen BE 2019-06-17 08:00:00+00:00 ... pm25 6.5 \u00b5g/m\u00b3\n\n2 Antwerpen BE 2019-06-17 07:00:00+00:00 ... pm25 18.5 \u00b5g/m\u00b3\n\n3 Antwerpen BE 2019-06-17 06:00:00+00:00 ... pm25 16.0 \u00b5g/m\u00b3\n\n4 Antwerpen BE 2019-06-17 05:00:00+00:00 ... pm25 7.5 \u00b5g/m\u00b3\n\n ... ... ... ... ... ... ...\n\n5267 London GB 2019-04-09 06:00:00+00:00 ... no2 41.0 \u00b5g/m\u00b3\n\n5268 London GB 2019-04-09 05:00:00+00:00 ... no2 41.0 \u00b5g/m\u00b3\n\n5269 London GB 2019-04-09 04:00:00+00:00 ... no2 41.0 \u00b5g/m\u00b3\n\n5270 London GB 2019-04-09 03:00:00+00:00 ... no2 67.0 \u00b5g/m\u00b3\n\n5271 London GB 2019-04-09 02:00:00+00:00 ... no2 67.0 \u00b5g/m\u00b3\n\n[5272 rows x 7 columns]\n\ndf.d_add_prefix_to_index_when_regex_match('five_', regular_expression='^5')\n\nOut[13]: \n\n city country date.utc ... parameter value unit\n\n0 Antwerpen BE 2019-06-18 06:00:00+00:00 ... pm25 18.0 \u00b5g/m\u00b3\n\n1 Antwerpen BE 2019-06-17 08:00:00+00:00 ... pm25 6.5 \u00b5g/m\u00b3\n\n2 Antwerpen BE 2019-06-17 07:00:00+00:00 ... pm25 18.5 \u00b5g/m\u00b3\n\n3 Antwerpen BE 2019-06-17 06:00:00+00:00 ... pm25 16.0 \u00b5g/m\u00b3\n\n4 Antwerpen BE 2019-06-17 05:00:00+00:00 ... pm25 7.5 \u00b5g/m\u00b3\n\n ... ... ... ... ... ... ...\n\nfive_5267 London GB 2019-04-09 06:00:00+00:00 ... no2 41.0 \u00b5g/m\u00b3\n\nfive_5268 London GB 2019-04-09 05:00:00+00:00 ... no2 41.0 \u00b5g/m\u00b3\n\nfive_5269 London GB 2019-04-09 04:00:00+00:00 ... no2 41.0 \u00b5g/m\u00b3\n\nfive_5270 London GB 2019-04-09 03:00:00+00:00 ... no2 67.0 \u00b5g/m\u00b3\n\nfive_5271 London GB 2019-04-09 02:00:00+00:00 ... no2 67.0 \u00b5g/m\u00b3\n\n[5272 rows x 7 columns]\n\n```\n\n\n\n### df.d_filter_df_by_regex_in_columns\n\n\n\n```python\n\ndf\n\nOut[14]: \n\n city country date.utc ... parameter value unit\n\n0 Antwerpen BE 2019-06-18 06:00:00+00:00 ... pm25 18.0 \u00b5g/m\u00b3\n\n1 Antwerpen BE 2019-06-17 08:00:00+00:00 ... pm25 6.5 \u00b5g/m\u00b3\n\n2 Antwerpen BE 2019-06-17 07:00:00+00:00 ... pm25 18.5 \u00b5g/m\u00b3\n\n3 Antwerpen BE 2019-06-17 06:00:00+00:00 ... pm25 16.0 \u00b5g/m\u00b3\n\n4 Antwerpen BE 2019-06-17 05:00:00+00:00 ... pm25 7.5 \u00b5g/m\u00b3\n\n ... ... ... ... ... ... ...\n\n5267 London GB 2019-04-09 06:00:00+00:00 ... no2 41.0 \u00b5g/m\u00b3\n\n5268 London GB 2019-04-09 05:00:00+00:00 ... no2 41.0 \u00b5g/m\u00b3\n\n5269 London GB 2019-04-09 04:00:00+00:00 ... no2 41.0 \u00b5g/m\u00b3\n\n5270 London GB 2019-04-09 03:00:00+00:00 ... no2 67.0 \u00b5g/m\u00b3\n\n5271 London GB 2019-04-09 02:00:00+00:00 ... no2 67.0 \u00b5g/m\u00b3\n\n[5272 rows x 7 columns]\n\ndf.d_filter_df_by_regex_in_columns('^[cu]')\n\nOut[15]: \n\n city country unit\n\n0 Antwerpen BE \u00b5g/m\u00b3\n\n1 Antwerpen BE \u00b5g/m\u00b3\n\n2 Antwerpen BE \u00b5g/m\u00b3\n\n3 Antwerpen BE \u00b5g/m\u00b3\n\n4 Antwerpen BE \u00b5g/m\u00b3\n\n ... ... ...\n\n5267 London GB \u00b5g/m\u00b3\n\n5268 London GB \u00b5g/m\u00b3\n\n5269 London GB \u00b5g/m\u00b3\n\n5270 London GB \u00b5g/m\u00b3\n\n5271 London GB \u00b5g/m\u00b3\n\n[5272 rows x 3 columns]\n\n```\n\n\n\n### df.d_filter_df_by_regex_in_index\n\n\n\n```python\n\ndf\n\nOut[16]: \n\n city country date.utc ... parameter value unit\n\n0 Antwerpen BE 2019-06-18 06:00:00+00:00 ... pm25 18.0 \u00b5g/m\u00b3\n\n1 Antwerpen BE 2019-06-17 08:00:00+00:00 ... pm25 6.5 \u00b5g/m\u00b3\n\n2 Antwerpen BE 2019-06-17 07:00:00+00:00 ... pm25 18.5 \u00b5g/m\u00b3\n\n3 Antwerpen BE 2019-06-17 06:00:00+00:00 ... pm25 16.0 \u00b5g/m\u00b3\n\n4 Antwerpen BE 2019-06-17 05:00:00+00:00 ... pm25 7.5 \u00b5g/m\u00b3\n\n ... ... ... ... ... ... ...\n\n5267 London GB 2019-04-09 06:00:00+00:00 ... no2 41.0 \u00b5g/m\u00b3\n\n5268 London GB 2019-04-09 05:00:00+00:00 ... no2 41.0 \u00b5g/m\u00b3\n\n5269 London GB 2019-04-09 04:00:00+00:00 ... no2 41.0 \u00b5g/m\u00b3\n\n5270 London GB 2019-04-09 03:00:00+00:00 ... no2 67.0 \u00b5g/m\u00b3\n\n5271 London GB 2019-04-09 02:00:00+00:00 ... no2 67.0 \u00b5g/m\u00b3\n\n[5272 rows x 7 columns]\n\ndf.d_filter_df_by_regex_in_index(r'^5\\d\\d0$')\n\nOut[17]: \n\n city country date.utc ... parameter value unit\n\n5000 London GB 2019-04-20 16:00:00+00:00 ... no2 48.0 \u00b5g/m\u00b3\n\n5010 London GB 2019-04-20 06:00:00+00:00 ... no2 33.0 \u00b5g/m\u00b3\n\n5020 London GB 2019-04-19 20:00:00+00:00 ... no2 58.0 \u00b5g/m\u00b3\n\n5030 London GB 2019-04-19 10:00:00+00:00 ... no2 44.0 \u00b5g/m\u00b3\n\n5040 London GB 2019-04-18 23:00:00+00:00 ... no2 61.0 \u00b5g/m\u00b3\n\n5050 London GB 2019-04-18 13:00:00+00:00 ... no2 49.0 \u00b5g/m\u00b3\n\n5060 London GB 2019-04-18 03:00:00+00:00 ... no2 50.0 \u00b5g/m\u00b3\n\n5070 London GB 2019-04-17 17:00:00+00:00 ... no2 54.0 \u00b5g/m\u00b3\n\n5080 London GB 2019-04-17 07:00:00+00:00 ... no2 51.0 \u00b5g/m\u00b3\n\n5090 London GB 2019-04-16 20:00:00+00:00 ... no2 83.0 \u00b5g/m\u00b3\n\n5100 London GB 2019-04-16 09:00:00+00:00 ... no2 66.0 \u00b5g/m\u00b3\n\n5110 London GB 2019-04-15 22:00:00+00:00 ... no2 47.0 \u00b5g/m\u00b3\n\n5120 London GB 2019-04-15 12:00:00+00:00 ... no2 27.0 \u00b5g/m\u00b3\n\n5130 London GB 2019-04-15 02:00:00+00:00 ... no2 32.0 \u00b5g/m\u00b3\n\n5140 London GB 2019-04-14 16:00:00+00:00 ... no2 23.0 \u00b5g/m\u00b3\n\n5150 London GB 2019-04-14 06:00:00+00:00 ... no2 35.0 \u00b5g/m\u00b3\n\n5160 London GB 2019-04-13 20:00:00+00:00 ... no2 29.0 \u00b5g/m\u00b3\n\n5170 London GB 2019-04-13 10:00:00+00:00 ... no2 45.0 \u00b5g/m\u00b3\n\n5180 London GB 2019-04-13 00:00:00+00:00 ... no2 29.0 \u00b5g/m\u00b3\n\n5190 London GB 2019-04-12 14:00:00+00:00 ... no2 39.0 \u00b5g/m\u00b3\n\n5200 London GB 2019-04-12 04:00:00+00:00 ... no2 33.0 \u00b5g/m\u00b3\n\n5210 London GB 2019-04-11 16:00:00+00:00 ... no2 34.0 \u00b5g/m\u00b3\n\n5220 London GB 2019-04-11 06:00:00+00:00 ... no2 46.0 \u00b5g/m\u00b3\n\n5230 London GB 2019-04-10 19:00:00+00:00 ... no2 35.0 \u00b5g/m\u00b3\n\n5240 London GB 2019-04-10 09:00:00+00:00 ... no2 35.0 \u00b5g/m\u00b3\n\n5250 London GB 2019-04-09 23:00:00+00:00 ... no2 38.0 \u00b5g/m\u00b3\n\n5260 London GB 2019-04-09 13:00:00+00:00 ... no2 56.0 \u00b5g/m\u00b3\n\n5270 London GB 2019-04-09 03:00:00+00:00 ... no2 67.0 \u00b5g/m\u00b3\n\n[28 rows x 7 columns]\n\n```\n\n\n\n### df.d_columns_upper, df.d_columns_lower,df.d_make_columns_dot_compatible,df.d_index_upper,df.d_index_lower\n\n\n\n```python\n\nprint(df.columns)\n\nprint(df.d_make_columns_dot_compatible().columns)\n\nprint(df.d_columns_upper().columns)\n\nprint(df.d_columns_lower().columns)\n\ndf2=df.copy()\n\ndf2.index = df2.parameter\n\nprint(df2.index)\n\nprint(df2.d_index_upper().index)\n\nprint(df2.d_index_lower().index)\n\nIndex(['city', 'country', 'date.utc', 'location', 'parameter', 'value',\n\n 'unit'],\n\n dtype='object')\n\nIndex(['city', 'country', 'date_utc', 'location', 'parameter', 'value',\n\n 'unit'],\n\n dtype='object')\n\nIndex(['CITY', 'COUNTRY', 'DATE_UTC', 'LOCATION', 'PARAMETER', 'VALUE',\n\n 'UNIT'],\n\n dtype='object')\n\nIndex(['city', 'country', 'date_utc', 'location', 'parameter', 'value',\n\n 'unit'],\n\n dtype='object')\n\nIndex(['pm25', 'pm25', 'pm25', 'pm25', 'pm25', 'pm25', 'pm25', 'pm25', 'pm25',\n\n 'pm25',\n\n ...\n\n 'no2', 'no2', 'no2', 'no2', 'no2', 'no2', 'no2', 'no2', 'no2', 'no2'],\n\n dtype='object', name='parameter', length=5272)\n\nIndex(['PM25', 'PM25', 'PM25', 'PM25', 'PM25', 'PM25', 'PM25', 'PM25', 'PM25',\n\n 'PM25',\n\n ...\n\n 'NO2', 'NO2', 'NO2', 'NO2', 'NO2', 'NO2', 'NO2', 'NO2', 'NO2', 'NO2'],\n\n dtype='object', length=5272)\n\nIndex(['pm25', 'pm25', 'pm25', 'pm25', 'pm25', 'pm25', 'pm25', 'pm25', 'pm25',\n\n 'pm25',\n\n ...\n\n 'no2', 'no2', 'no2', 'no2', 'no2', 'no2', 'no2', 'no2', 'no2', 'no2'],\n\n dtype='object', length=5272)\n\n```\n\n\n\n### df.d_natsort_index\n\n\n\n```python\n\ndf2 = df.sample(len(df)).copy()\n\ndftempindex = df2.index[:2500].to_list()\n\ntempvalue = df2.loc[dftempindex].parameter.copy()\n\ntempvalue = tempvalue.apply(lambda x: str(x).upper())\n\ndf2.loc[dftempindex, 'parameter'] = tempvalue\n\nprint(df2)\n\ndf2.index = df2.parameter\n\nprint(df2.d_natsort_index())\n\nfor a,b,c,d in zip(df2.d_natsort_index(sort_numbers_after_non_numbers=True).index.to_list(), df2.d_natsort_index(lowercase_first=True).index.to_list(),df2.d_natsort_index(group_lower_and_uppercase=True).index.to_list(),df2.d_natsort_index(uppercase_first=True).index.to_list()):\n\n print(a,b,c,d)\n\n\n\nno2 NO2 no2 no2\n\nno2 NO2 no2 no2\n\nno2 NO2 no2 no2\n\nno2 NO2 no2 no2\n\nno2 NO2 no2 no2\n\nno2 NO2 no2 no2\n\nno2 NO2 no2 no2\n\nno2 NO2 no2 no2\n\nno2 NO2 no2 no2\n\nno2 NO2 no2 no2\n\nno2 NO2 no2 no2\n\nno2 NO2 no2 no2\n\nno2 NO2 no2 no2\n\nno2 NO2 no2 no2\n\nno2 NO2 no2 no2\n\nno2 NO2 no2 no2\n\nno2 NO2 no2 no2\n\nno2 NO2 no2 no2\n\nno2 NO2 no2 no2\n\nno2 NO2 no2 no2\n\nno2 NO2 no2 no2\n\nno2 NO2 no2 no2\n\nno2 NO2 PM25 no2\n\nno2 NO2 PM25 no2\n\nno2 NO2 PM25 no2\n\nno2 NO2 PM25 no2\n\nno2 NO2 PM25 no2\n\nno2 NO2 PM25 no2\n\nno2 NO2 PM25 no2\n\nno2 NO2 PM25 no2\n\nno2 NO2 PM25 no2\n\nno2 NO2 PM25 no2\n\nno2 NO2 PM25 no2\n\nno2 NO2 PM25 no2\n\nno2 NO2 PM25 no2\n\nno2 NO2 PM25 no2\n\nno2 NO2 PM25 no2\n\nno2 NO2 PM25 no2\n\nno2 NO2 PM25 no2\n\nno2 NO2 PM25 no2\n\nno2 NO2 PM25 no2\n\nno2 NO2 PM25 no2\n\nno2 NO2 PM25 no2\n\nno2 NO2 PM25 no2\n\nno2 NO2 PM25 no2\n\nno2 NO2 PM25 no2\n\nno2 NO2 PM25 no2\n\nno2 NO2 PM25 no2\n\nno2 NO2 PM25 no2\n\n```\n\n\n\n### df.d_natsort_df_by_column\n\n\n\n```python\n\ndf\n\nOut[3]: \n\n city country date.utc ... parameter value unit\n\n0 Antwerpen BE 2019-06-18 06:00:00+00:00 ... pm25 18.0 \u00b5g/m\u00b3\n\n1 Antwerpen BE 2019-06-17 08:00:00+00:00 ... pm25 6.5 \u00b5g/m\u00b3\n\n2 Antwerpen BE 2019-06-17 07:00:00+00:00 ... pm25 18.5 \u00b5g/m\u00b3\n\n3 Antwerpen BE 2019-06-17 06:00:00+00:00 ... pm25 16.0 \u00b5g/m\u00b3\n\n4 Antwerpen BE 2019-06-17 05:00:00+00:00 ... pm25 7.5 \u00b5g/m\u00b3\n\n ... ... ... ... ... ... ...\n\n5267 London GB 2019-04-09 06:00:00+00:00 ... no2 41.0 \u00b5g/m\u00b3\n\n5268 London GB 2019-04-09 05:00:00+00:00 ... no2 41.0 \u00b5g/m\u00b3\n\n5269 London GB 2019-04-09 04:00:00+00:00 ... no2 41.0 \u00b5g/m\u00b3\n\n5270 London GB 2019-04-09 03:00:00+00:00 ... no2 67.0 \u00b5g/m\u00b3\n\n5271 London GB 2019-04-09 02:00:00+00:00 ... no2 67.0 \u00b5g/m\u00b3\n\n[5272 rows x 7 columns]\n\ndf.d_natsort_df_by_column('date.utc')\n\nOut[4]: \n\n city country date.utc ... parameter value unit\n\n176 Antwerpen BE 2019-04-09 01:00:00+00:00 ... pm25 76.0 \u00b5g/m\u00b3\n\n3500 Paris FR 2019-04-09 01:00:00+00:00 ... no2 24.4 \u00b5g/m\u00b3\n\n3663 Antwerpen BE 2019-04-09 01:00:00+00:00 ... no2 22.5 \u00b5g/m\u00b3\n\n175 Antwerpen BE 2019-04-09 02:00:00+00:00 ... pm25 91.5 \u00b5g/m\u00b3\n\n1824 London GB 2019-04-09 02:00:00+00:00 ... pm25 42.0 \u00b5g/m\u00b3\n\n ... ... ... ... ... ... ...\n\n1827 Paris FR 2019-06-20 22:00:00+00:00 ... no2 26.5 \u00b5g/m\u00b3\n\n178 London GB 2019-06-20 23:00:00+00:00 ... pm25 7.0 \u00b5g/m\u00b3\n\n1826 Paris FR 2019-06-20 23:00:00+00:00 ... no2 21.8 \u00b5g/m\u00b3\n\n177 London GB 2019-06-21 00:00:00+00:00 ... pm25 7.0 \u00b5g/m\u00b3\n\n1825 Paris FR 2019-06-21 00:00:00+00:00 ... no2 20.0 \u00b5g/m\u00b3\n\n[5272 rows x 7 columns]\n\n```\n\n\n\n### df.d_rename_columns / df.d_rename_index\n\n\n\n```python\n\ndf = pd.read_csv( \"https://github.com/pandas-dev/pandas/raw/main/doc/data/titanic.csv\")\n\n\n\nprint(df)\n\n PassengerId Survived Pclass ... Fare Cabin Embarked\n\n0 1 0 3 ... 7.2500 NaN S\n\n1 2 1 1 ... 71.2833 C85 C\n\n2 3 1 3 ... 7.9250 NaN S\n\n3 4 1 1 ... 53.1000 C123 S\n\n4 5 0 3 ... 8.0500 NaN S\n\n.. ... ... ... ... ... ... ...\n\n886 887 0 2 ... 13.0000 NaN S\n\n887 888 1 1 ... 30.0000 B42 S\n\n888 889 0 3 ... 23.4500 NaN S\n\n889 890 1 1 ... 30.0000 C148 C\n\n890 891 0 3 ... 7.7500 NaN Q\n\n\n\n\n\ndf.d_rename_columns(Fare='Embarked',Embarked='Fare',Cabin='cabin2')\n\ndf.d_rename_index({1: 1000000,2:50022})\n\nprint(df)\n\n\n\n PassengerId Survived Pclass ... Embarked cabin2 Fare\n\n0 1 0 3 ... 7.2500 NaN S\n\n1000000 2 1 1 ... 71.2833 C85 C\n\n50022 3 1 3 ... 7.9250 NaN S\n\n3 4 1 1 ... 53.1000 C123 S\n\n4 5 0 3 ... 8.0500 NaN S\n\n ... ... ... ... ... ... ...\n\n886 887 0 2 ... 13.0000 NaN S\n\n887 888 1 1 ... 30.0000 B42 S\n\n888 889 0 3 ... 23.4500 NaN S\n\n889 890 1 1 ... 30.0000 C148 C\n\n890 891 0 3 ... 7.7500 NaN Q\n\n[891 rows x 12 columns]\n\n\n\ndf.d_rename_columns({'Embarked': 'Fare', 'Fare' : 'Embarked', 'cabin2' : 'Cabin'})\n\ndf.index = df.index.astype('string')\n\ndf.index = 'a' + df.index\n\ndf.d_rename_index(a1000000= 1,a50022=2)\n\nprint(df)\n\n\n\n PassengerId Survived Pclass ... Fare Cabin Embarked\n\na0 1 0 3 ... 7.2500 NaN S\n\n1 2 1 1 ... 71.2833 C85 C\n\n2 3 1 3 ... 7.9250 NaN S\n\na3 4 1 1 ... 53.1000 C123 S\n\na4 5 0 3 ... 8.0500 NaN S\n\n ... ... ... ... ... ... ...\n\na886 887 0 2 ... 13.0000 NaN S\n\na887 888 1 1 ... 30.0000 B42 S\n\na888 889 0 3 ... 23.4500 NaN S\n\na889 890 1 1 ... 30.0000 C148 C\n\na890 891 0 3 ... 7.7500 NaN Q\n\n```\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Some useful methods for columns / index in Pandas DataFrames",
"version": "0.13",
"project_urls": {
"Homepage": "https://github.com/hansalemaos/a_pandas_ex_columns_and_index"
},
"split_keywords": [
"pandas",
"numpy",
"dataframe",
"series",
"regex",
"natsort"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "79ec99b936f293932c1f32c0598f48c5db2b4022445b03596a2f0165d536b4c9",
"md5": "7fe4f4db5573ece4aaf27a180e93aef6",
"sha256": "11ffaffdc851db9fd35fd1642b4199a0a39c19560b76394f2ea46b31f95a97bc"
},
"downloads": -1,
"filename": "a_pandas_ex_columns_and_index-0.13-py3-none-any.whl",
"has_sig": false,
"md5_digest": "7fe4f4db5573ece4aaf27a180e93aef6",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 13218,
"upload_time": "2022-10-08T03:41:34",
"upload_time_iso_8601": "2022-10-08T03:41:34.710951Z",
"url": "https://files.pythonhosted.org/packages/79/ec/99b936f293932c1f32c0598f48c5db2b4022445b03596a2f0165d536b4c9/a_pandas_ex_columns_and_index-0.13-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "d184b7e02dd5436b2f85a5b2bdfd5695b1e6b2785949aed88475a44ed057fe8d",
"md5": "d1445fd6a84e7e5eba7217830a124098",
"sha256": "6e5419e62d9a8eb9d33b16b5e9ea3e97d421a67ad1afd6fb3c55766d812762a0"
},
"downloads": -1,
"filename": "a_pandas_ex_columns_and_index-0.13.tar.gz",
"has_sig": false,
"md5_digest": "d1445fd6a84e7e5eba7217830a124098",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 16020,
"upload_time": "2022-10-08T03:41:37",
"upload_time_iso_8601": "2022-10-08T03:41:37.386240Z",
"url": "https://files.pythonhosted.org/packages/d1/84/b7e02dd5436b2f85a5b2bdfd5695b1e6b2785949aed88475a44ed057fe8d/a_pandas_ex_columns_and_index-0.13.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2022-10-08 03:41:37",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "hansalemaos",
"github_project": "a_pandas_ex_columns_and_index",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"requirements": [
{
"name": "natsort",
"specs": []
},
{
"name": "numpy",
"specs": []
},
{
"name": "pandas",
"specs": []
},
{
"name": "regex",
"specs": []
}
],
"lcname": "a-pandas-ex-columns-and-index"
}