python - 删除数据框单元格中的任何值
问题描述
我想转换数据集类型。但我不能这样做,因为我的数据集中有两个点。我正在使用pd.apply(pd.to_numeric)
代码。我得到的错误代码如下;
ValueError: Unable to parse string "1.232.2" at position 1
我的数据集是这样的;
Price Value
1.232.2 1.235.3
2.345.2 1.234.2
3.343.5 5.433.3
我必须删除第一个点。例如;
Price Value
1232.2 1235.3
2345.2 1234.2
3343.5 5433.3
我在等待帮助。谢谢你。
解决方案
这是一种方法。
将字符串转换为浮点格式(多点到单点)
你可以做一个正则表达式来解决这个问题。
正则表达式:'\.(?=.*\.)'
解释:
'\.
--> 查找文字.
(?=.*\.)'
--> 排除除最后一个以外的所有内容.
对于每个找到的,替换为''
代码是:
df['Price'] = df['Price'].str.replace('\.(?=.*\.)', '',regex=True)
df['Value'] = df['Value'].str.replace('\.(?=.*\.)', '',regex=True)
如果还想转成数字,可以直接给:
df['Price'] = pd.to_numeric(df['Price'].str.replace('\.(?=.*\.)', '',regex=True))
df['Value'] = pd.to_numeric(df['Value'].str.replace('\.(?=.*\.)', '',regex=True))
其输出将是:
在清理 DataFrame 之前:
Price Value
0 1.232.2 1.235.3
1 2.345.2 1.234.2
2 3.343.5 5.433.3
3 123.45 456.25.5
4 0.825 0.0.0
5 0.0.0.2 5.5.5
6 1234 4567
7 NaN NaN
清理 DataFrame 后:
Price Value
0 1232.2 1235.3
1 2345.2 1234.2
2 3343.5 5433.3
3 123.45 45625.5
4 0.825 00.0
5 000.2 55.5
6 1234 4567
7 NaN NaN
解决方案的 pd.numeric() 版本将如下所示:
在 Cleanins DataFrame 之后:
注意:它将所有值转换为 3 位小数,因为其中一个值有 3 位小数。
Price Value
0 1232.200 1235.3
1 2345.200 1234.2
2 3343.500 5433.3
3 123.450 45625.5
4 0.825 0.0
5 0.200 55.5
6 1234.000 4567.0
7 NaN NaN
如果数据中有多个句点 (.),则丢弃数据
如果要处理数据框中的所有列,可以使用applymap()
,如果要处理特定列,请使用 apply。也用于pd.isnull()
检查数据是否存在NaN
,以便您可以忽略处理该数据。
下面的代码地址为 NaN、不带小数位的数字、带一个句点的数字、带多个句点的数字。该代码假定列中的数据是 NaN 或带有数字和句点的字符串。它假定没有字母或非数字字符(点除外)。如果您只需要代码来检查数字,请告诉我。
该代码还假定您要丢弃前导数字。如果您确实想连接数字,则需要实施不同的解决方案(例如:1.2345.67
将被替换为2345.67
并将1
被丢弃。示例#2:1.2.3.4.5
将被替换为4.5
while discarding 1.2.3
。如果这不是您想要的,我们需要更改代码。
您可以执行以下操作:
import pandas as pd
import numpy as np
df = pd.DataFrame({'Price': ['1.232.2', '2.345.2', '3.343.5', '123.45', '0.825','0.0.0.2', '1234',np.NaN],
'Value': ['1.235.3', '1.234.2', '5.433.3', '456.25.5','0.0.0','5.5.5', '4567',np.NaN]})
print (df)
def remove_dots(x):
return x if pd.isnull(x) else '.'.join(x.rsplit('.',2)[-2:])
df = df.applymap(remove_dots)
print (df)
其输出将是:
在清理 DataFrame 之前:
Price Value
0 1.232.2 1.235.3
1 2.345.2 1.234.2
2 3.343.5 5.433.3
3 123.45 456.25.5
4 0.825 0.0.0
5 0.0.0.2 5.5.5
6 1234 4567
7 NaN NaN
清理 DataFrame 后:
Price Value
0 232.2 235.3
1 345.2 234.2
2 343.5 433.3
3 123.45 25.5
4 0.825 0.0
5 0.2 5.5
6 1234 4567
7 NaN NaN
如果您只想更改特定列,则可以使用 apply。
df['Price'] = df['Price'].apply(lambda x: x if pd.isnull(x) else '.'.join(x.rsplit('.',2)[-2:]))
df['Value'] = df['Value'].apply(lambda x: x if pd.isnull(x) else '.'.join(x.rsplit('.',2)[-2:]))
print(df)
之前和之后将是相同的:
在清理 DataFrame 之前:
Price Value
0 1.232.2 1.235.3
1 2.345.2 1.234.2
2 3.343.5 5.433.3
3 123.45 456.25.5
4 0.825 0.0.0
5 0.0.0.2 5.5.5
6 1234 4567
7 NaN NaN
清理 DataFrame 后:
Price Value
0 232.2 235.3
1 345.2 234.2
2 343.5 433.3
3 123.45 25.5
4 0.825 0.0
5 0.2 5.5
6 1234 4567
7 NaN NaN
推荐阅读
- r - 在 R 中为三种不同的后验分布添加带有 ggplot 的标签
- python - 如何编写一个在文件中收集值的 Prometheus 导出器?
- visual-studio-code - 如何使用新的 vscode.task 以编程方式创建“dependsOn”任务
- checkbox - 您如何抑制复选框和单选按钮的标准 HTML 5 外观以支持 Ext JS 7 经典中的自定义外观?
- react-native - 如果打开了其他选项卡,请关闭其他选项卡(react-native-collapsible)
- macos - 如何在 MacOS 上的 MARS 中打开保存的 .asm 文件
- gradle - 管道任务报告生成器失败
- python - 在 subprocess.call(r'net use z: ' ) 函数中使用变量来映射网络驱动器
- android - 如何解决暂停函数只能在协程体内调用
- android - GestureDetector 未注册双击