Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.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 更改CSV中的每个单元格_Python_Csv - Fatal编程技术网

Python 更改CSV中的每个单元格

Python 更改CSV中的每个单元格,python,csv,Python,Csv,在我的CSV文件中,我有以下内容: Name Installs ----|------------------- |a 4+ |b 15+ |c 60+ |d 5+ 如何使用pandas遍历Installs列以删除末尾的加号?并将内容保存到现有的CSV文件中。类似于此的操作应该可以: df = pd.read_csv("your_file_name_here.csv") df["I

在我的CSV文件中,我有以下内容:

     Name    Installs 
----|-------------------
    |a         4+
    |b         15+
    |c         60+
    |d         5+

如何使用pandas遍历Installs列以删除末尾的加号?并将内容保存到现有的CSV文件中。

类似于此的操作应该可以:

df = pd.read_csv("your_file_name_here.csv")
df["Installs"] = df["Installs"].apply(lambda x: x.replace("x","")
df.to_csv("your_new_file_path_here.csv")
您可以使用
df[“安装”].str.strip(“+”)
str.rstrip(“+”)

Ex:

import pandas as pd

df = pd.DataFrame({"Name": ['a', 'b', 'c', 'd'], "Installs": ["4+", "15+", "60+", "5+"]})
df["Installs"] = df["Installs"].str.strip("+")
print(df)
  Installs Name
0        4    a
1       15    b
2       60    c
3        5    d
输出:

import pandas as pd

df = pd.DataFrame({"Name": ['a', 'b', 'c', 'd'], "Installs": ["4+", "15+", "60+", "5+"]})
df["Installs"] = df["Installs"].str.strip("+")
print(df)
  Installs Name
0        4    a
1       15    b
2       60    c
3        5    d

Series.str[:-1]
使用或切掉它:

df['Installs']=df['Installs'].str[:-1]
现在:

print(df)
是:

如果要将它们作为整数,请执行以下操作:

df['Installs']=df['Installs'].str[:-1].astype(int)

您也可以使用
str.rstrip()
手动执行此操作:

这将提供以下output.csv:

您也可以在此处使用
str.rsplit()

o.write(line.rsplit('+', 1)[0] + '\n')

你能解释一下你的答案吗?
     Name    Installs
----|-------------------
    |a         4
    |b         15
    |c         60
    |d         5
o.write(line.rsplit('+', 1)[0] + '\n')