Skip to content

Commit 6bc2597

Browse files
committed
added rename
1 parent 29aa7ce commit 6bc2597

File tree

3 files changed

+105
-5
lines changed

3 files changed

+105
-5
lines changed

README.md

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ df = pd.read_csv("https://github.com/pandas-dev/pandas/raw/main/doc/data/air_qua
2929
- df.d_natsort_index
3030
- df.d_natort_columns
3131
- df.d_natsort_df_by_column
32+
- d_rename_index
33+
- d_rename_columns
3234

3335
**All methods added to pandas have one of this prefixes:**
3436

@@ -79,7 +81,6 @@ Index(['country', 'city', 'date.utc', 'location', 'parameter', 'value',
7981
5270 GB London 2019-04-09 03:00:00+00:00 ... no2 67.0 µg/
8082
5271 GB London 2019-04-09 02:00:00+00:00 ... no2 67.0 µg/
8183
[5272 rows x 7 columns]
82-
8384
```
8485

8586
### df.ds_sort_by_str_length
@@ -115,7 +116,6 @@ print(df2)
115116
3548 Antwerpen BE 2019-05-19 21:00:00+00:00 ... no2 12.5 µg/
116117
0 Antwerpen BE 2019-06-18 06:00:00+00:00 ... pm25 18.0 µg/
117118
[5272 rows x 7 columns]
118-
119119
```
120120

121121
### d_insert_column_before_another
@@ -151,7 +151,6 @@ Out[7]:
151151
5270 London GB 2019-04-09 03:00:00+00:00 ... LondonGB 67.0 µg/
152152
5271 London GB 2019-04-09 02:00:00+00:00 ... LondonGB 67.0 µg/
153153
[5272 rows x 8 columns]
154-
155154
```
156155

157156
### df.ds_reverse
@@ -384,7 +383,6 @@ Index(['pm25', 'pm25', 'pm25', 'pm25', 'pm25', 'pm25', 'pm25', 'pm25', 'pm25',
384383
...
385384
'no2', 'no2', 'no2', 'no2', 'no2', 'no2', 'no2', 'no2', 'no2', 'no2'],
386385
dtype='object', length=5272)
387-
388386
```
389387

390388
### df.d_natsort_index
@@ -486,3 +484,61 @@ Out[4]:
486484
1825 Paris FR 2019-06-21 00:00:00+00:00 ... no2 20.0 µg/
487485
[5272 rows x 7 columns]
488486
```
487+
488+
### df.d_rename_columns / df.d_rename_index
489+
490+
```python
491+
df = pd.read_csv( "https://github.com/pandas-dev/pandas/raw/main/doc/data/titanic.csv")
492+
493+
print(df)
494+
PassengerId Survived Pclass ... Fare Cabin Embarked
495+
0 1 0 3 ... 7.2500 NaN S
496+
1 2 1 1 ... 71.2833 C85 C
497+
2 3 1 3 ... 7.9250 NaN S
498+
3 4 1 1 ... 53.1000 C123 S
499+
4 5 0 3 ... 8.0500 NaN S
500+
.. ... ... ... ... ... ... ...
501+
886 887 0 2 ... 13.0000 NaN S
502+
887 888 1 1 ... 30.0000 B42 S
503+
888 889 0 3 ... 23.4500 NaN S
504+
889 890 1 1 ... 30.0000 C148 C
505+
890 891 0 3 ... 7.7500 NaN Q
506+
507+
508+
df.d_rename_columns(Fare='Embarked',Embarked='Fare',Cabin='cabin2')
509+
df.d_rename_index({1: 1000000,2:50022})
510+
print(df)
511+
512+
PassengerId Survived Pclass ... Embarked cabin2 Fare
513+
0 1 0 3 ... 7.2500 NaN S
514+
1000000 2 1 1 ... 71.2833 C85 C
515+
50022 3 1 3 ... 7.9250 NaN S
516+
3 4 1 1 ... 53.1000 C123 S
517+
4 5 0 3 ... 8.0500 NaN S
518+
... ... ... ... ... ... ...
519+
886 887 0 2 ... 13.0000 NaN S
520+
887 888 1 1 ... 30.0000 B42 S
521+
888 889 0 3 ... 23.4500 NaN S
522+
889 890 1 1 ... 30.0000 C148 C
523+
890 891 0 3 ... 7.7500 NaN Q
524+
[891 rows x 12 columns]
525+
526+
df.d_rename_columns({'Embarked': 'Fare', 'Fare' : 'Embarked', 'cabin2' : 'Cabin'})
527+
df.index = df.index.astype('string')
528+
df.index = 'a' + df.index
529+
df.d_rename_index(a1000000= 1,a50022=2)
530+
print(df)
531+
532+
PassengerId Survived Pclass ... Fare Cabin Embarked
533+
a0 1 0 3 ... 7.2500 NaN S
534+
1 2 1 1 ... 71.2833 C85 C
535+
2 3 1 3 ... 7.9250 NaN S
536+
a3 4 1 1 ... 53.1000 C123 S
537+
a4 5 0 3 ... 8.0500 NaN S
538+
... ... ... ... ... ... ...
539+
a886 887 0 2 ... 13.0000 NaN S
540+
a887 888 1 1 ... 30.0000 B42 S
541+
a888 889 0 3 ... 23.4500 NaN S
542+
a889 890 1 1 ... 30.0000 C148 C
543+
a890 891 0 3 ... 7.7500 NaN Q
544+
```

__init__.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import numpy as np
22
import regex
3+
from flatten_any_dict_iterable_or_whatsoever import fla_tu
34
from natsort import ns, index_natsorted, natsorted
45
import pandas as pd
56
from typing import Union
@@ -420,6 +421,47 @@ def sort_whole_df_with_natsort(
420421
)
421422
return df2.copy()
422423

424+
def check_if_equal(vara, varb):
425+
if str(type(vara)) == str(type(varb)):
426+
if str(vara) == str(varb):
427+
if len(set(dir(vara)) ^ set(dir(varb))) == 0:
428+
return True
429+
return False
430+
431+
def fladicol(*args, **kwargs):
432+
return {x[1][-1]:x[0] for x in (fla_tu([args, kwargs]))}
433+
434+
def rename_columns(*args, **kwargs):
435+
kwargs['______col____or____index'] = 'columns'
436+
kwargs['______df___'] = args[0]
437+
return rename_columnsindex(*args, **kwargs)
438+
439+
def rename_index(*args, **kwargs):
440+
kwargs['______col____or____index'] = 'index'
441+
kwargs['______df___'] = args[0]
442+
return rename_columnsindex(*args, **kwargs)
443+
444+
def rename_columnsindex(*args, **kwargs):
445+
df2 = kwargs['______df___']
446+
iscols = kwargs['______col____or____index'] == 'columns'
447+
if iscols:
448+
startcols = [x for x in df2.columns.to_list()]
449+
else:
450+
startcols = [x for x in df2.index.to_list()]
451+
kwargs = {x[0]:x[1] for x in kwargs.items() if x[0] not in ['______col____or____index','______df___']}
452+
renamedict = fladicol(*args[1:],**kwargs)
453+
newdict= {}
454+
for ini,col in enumerate(startcols):
455+
if col not in renamedict:
456+
newdict[ini] = col
457+
else:
458+
newdict[ini] = renamedict[col]
459+
finallist = [x[1] for x in sorted(newdict.items())]
460+
if iscols:
461+
df2.columns = finallist
462+
else:
463+
df2.index = finallist
464+
return df2
423465

424466
def pd_add_index_and_columns():
425467
DataFrame.d_swap_2_columns = change_places_2_columns
@@ -444,3 +486,5 @@ def pd_add_index_and_columns():
444486
DataFrame.d_natsort_index = qq_d_sort_index
445487
DataFrame.d_natort_columns = qq_d_sort_columns
446488
DataFrame.d_natsort_df_by_column = sort_whole_df_with_natsort
489+
DataFrame.d_rename_columns = rename_columns
490+
DataFrame.d_rename_index = rename_index

thirdparty.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)