简体   繁体   English

删除特定字符后的所有字符,而不是字符本身,在整列中,python

[英]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.

相关问题 如何删除python中特定字符后的所有字符? - How to remove all characters after a specific character in python? 如何删除df中整个列的某个字符后的所有内容? - How to delete everything after a certain character for whole column in df? python:如何删除字符串中特定字符之后的每个字符? - python: How to delete every character after a specific character in a string? Python - 删除列表中字符串中标识的特定字符之后的所有字符 - Python - Removing all characters on right after a specific character identified in string within list Pandas:删除 dataframe 列中特定字符之前的所有字符 - Pandas: Remove all characters before a specific character in a dataframe column 批量删除文件名中特定字符之后的所有字符 - Batch remove all characters after a specific character in filename 如何删除 Python 中特定字符之前的所有字符? - How to remove all characters before a specific character in Python? Python-如何删除特定单词/字符后的其余字符串 - Python - How to delete rest of string after a specific word/character 如何使用 Python 3 删除某个字符后的所有字符 - How to remove all characters after a certain character using Python 3 如何找到字符索引并删除其后的字符 - How to find the index of character and delete the characters after it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM