简体   繁体   中英

Find index of cell in dateframe

I would like to modify the cell value based on its size.

If the dateframe is as below:

  A         B    C
 25802523   X1   2
 M25JK0010  Y1   1
 K25JK0010  Y2   1

I would like to modify the column 'A' and insert to another column. For example, if the first cell value the size of column A is 8. I would like to break it and get least 5 values, similarly others depend on their sizes of each cell.

Is there any way I'm able to do this?

You can do this:

t = pd.DataFrame({'A': ['25802523', 'M25JK00010', 'KRJOJR4445'],
                  'size': [2, 1, 8]} )

Define a dictionary of your desired final length based on the corresponding size. Here if the size is 8 I will take the 5 last characters

size_dict = {8: 5, 2: 3, 1: 4}

Then use a simple pandas apply

t['A_bis'] = t.apply(lambda x: x['A'][len(x['A']) - size_dict[x['size']]:], axis=1)

The result is

0      523 >> 3 last characters (key 2)
1     0010 >> 4 last characters (key 1)
2    R4445 >> 5 last characters (key 8)

Another approach to do this:

Sample df:

t = pd.DataFrame({'A': ['25802523', 'M25JK00010', 'KRJOJR4445']})

Get the count of each elements of A:

t['Count'] =(t['A'].apply(len))

Then write a condition to replace:

t.loc[t.Count == 8, 'Number'] = t['A'].str[-5:]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM