如何完成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('%')+'%'