Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何完成Python数据帧列中缺少的字符_Python_Python 3.x_Pandas_Dataframe - Fatal编程技术网

如何完成Python数据帧列中缺少的字符

如何完成Python数据帧列中缺少的字符,python,python-3.x,pandas,dataframe,Python,Python 3.x,Pandas,Dataframe,我有以下数据帧: d = {'col1': ["1", "2", "3", "4"], 'col2': ["5%", "6", "7%", "8%"]} df = pd.DataFrame(data=d) df col1 col2 0 1 5% 1 2 6 2 3 7% 3 4

我有以下数据帧:

d = {'col1': ["1", "2", "3", "4"], 'col2': ["5%", "6", "7%", "8%"]}
df = pd.DataFrame(data=d)
df

   col1  col2
0     1    5%
1     2     6
2     3    7%
3     4    8%
在col2的某些行中,可以有结尾没有%符号的数字。我事先不知道我在哪一行遇到了这个问题。我需要确保所有数字在col2中都有%符号

有没有一种方法可以在Python中不通过数据帧循环执行此操作?

试试:

import numpy as np
condition=[df.col2.str.contains('%'),~df.col2.str.contains('%')]
choices=[df.col2, df.col2 +"%"]
df.col2=np.select(condition,choices)

  col1 col2
0    1   5%
1    2   6%
2    3   7%
3    4   8%
或者,您可以使用列表理解-它们非常有效,特别是对于字符串:

df['col2'] = [f"{entry}%" if not entry.endswith("%") else entry 
              for entry in df.col2]
尝试:

或者,您可以使用列表理解-它们非常有效,特别是对于字符串:

df['col2'] = [f"{entry}%" if not entry.endswith("%") else entry 
              for entry in df.col2]

与sammywemmy的答案类似。np.where通常是我在这种情况下的首选选项:

df['col2'] = np.where(~(df['col2'].str.contains('%')), df['col2'] + '%', df['col2'])


与sammywemmy的答案类似。np.where通常是我在这种情况下的首选选项:

df['col2'] = np.where(~(df['col2'].str.contains('%')), df['col2'] + '%', df['col2'])

如果右侧存在“%”符号,可以将其删除,然后在所有位置添加一个

df['col2'] = df['col2'].str.rstrip('%')+'%'
如果右侧存在“%”符号,可以将其删除,然后在所有位置添加一个

df['col2'] = df['col2'].str.rstrip('%')+'%'