[英]delete all characters after specific character, not character itself, in whole column, python
I have a data frame that I made into a dict for reproducibility purposes.我有一个数据框,为了可重复性的目的,我把它做成了一个 dict。 I want to delete all characters after the last "1" in the "index" column, not deleting "1" itself.我想删除“索引”列中最后一个“1”之后的所有字符,而不是删除“1”本身。 Does someone know how to quickly do this?有人知道如何快速做到这一点吗? I tried the following but it also deleted the "1":我尝试了以下操作,但它也删除了“1”:
df1['index'] = [x.split('1')[-1] for x in df1['index']]
{0: {'1': 0.001549586776859504,
'10': 0.001549586776859504,
'11': 0.00017217630853994488,
'12': 4.304407713498622e-05,
'13': 0.00012913223140495865},
1: {'1': 0.000387396694214876,
'10': 0.000387396694214876,
'11': nan,
'12': nan,
'13': nan},
2: {'1': 0.001162190082644628,
'10': 0.001162190082644628,
'11': 9.838646202282564e-05,
'12': 2.459661550570641e-05,
'13': 7.378984651711923e-05},
3: {'1': 0.015883264462809916,
'10': 0.015883264462809916,
'11': 0.0006149153876426602,
'12': 0.00015372884691066505,
'13': 0.0004611865407319952},
4: {'1': 0.00387396694214876,
'10': 0.00387396694214876,
'11': 0.00012298307752853205,
'12': 3.0745769382133014e-05,
'13': 9.223730814639904e-05},
5: {'1': 0.001549586776859504,
'10': 0.001549586776859504,
'11': nan,
'12': nan,
'13': nan},
6: {'1': 0.005423553719008264,
'10': 0.005423553719008264,
'11': 0.0002951593860684769,
'12': 7.378984651711923e-05,
'13': 0.00022136953955135768},
7: {'1': 0.001549586776859504,
'10': 0.001549586776859504,
'11': 0.00017217630853994488,
'12': 4.304407713498622e-05,
'13': 0.00012913223140495865},
8: {'1': 0.001162190082644628,
'10': 0.001162190082644628,
'11': nan,
'12': nan,
'13': nan},
9: {'1': 0.001549586776859504,
'10': 0.001549586776859504,
'11': 7.378984651711923e-05,
'12': 1.8447461629279807e-05,
'13': 5.534238488783942e-05},
'index': {'1': '00', '10': '000', '11': '0', '12': '20', '13': '30'}}
this solution convert int to string then search for first number different number from zero then take the partition you need (from begining to the position of the number different from zero) and convert it back to float.此解决方案将 int 转换为字符串,然后搜索第一个与零不同的数字,然后获取您需要的分区(从开始到与零不同的数字的位置)并将其转换回浮点数。 happy coding快乐编码
df1['index']=[float(x[:x.find(x.split(".")[-1].replace("0","")[0])+1]) for x in list(map(str,df1['index']))]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.