【问题标题】:How to prepend a string to a column in csv如何将字符串添加到csv中的列
【发布时间】:2020-11-14 21:26:01
【问题描述】:

我在in.csv 中有一个包含 5 列的 csv,我想在同一个文件中将第 2 列中的所有数据添加到“文本”中,以便该行

data1 data2 data3 data4 data5

变成

data1 textdata2 data3 data4 data5

我认为使用正则表达式可能是个好主意,但我不确定如何继续

编辑:

按照bigbounty的回答进行后,我使用了以下脚本:

import pandas as pd
df = pd.read_csv("in.csv")
df["id_str"] = str("text" + str(df["id_str"]))
df.to_csv("new_in.csv", index=False)

我的in.out 文件是这样的:

s_no,id_str,screen_name
1,1.15017060743203E+018,lorem
2,1.15015544419693E+018,ipsum
3,1.15015089995785E+018,dolor
4,1.15015054311063E+018,sit

运行脚本后new_in.csv文件为:

s_no,id_str,screen_name
1,"text0    1.150171e+18
1    1.150155e+18
2    1.150151e+18
3    1.150151e+18
Name: id_str, dtype: float64",lorem
2,"text0    1.150171e+18
1    1.150155e+18
2    1.150151e+18
3    1.150151e+18
Name: id_str, dtype: float64",ipsum
3,"text0    1.150171e+18
1    1.150155e+18
2    1.150151e+18
3    1.150151e+18
Name: id_str, dtype: float64",dolor
4,"text0    1.150171e+18
1    1.150155e+18
2    1.150151e+18
3    1.150151e+18
Name: id_str, dtype: float64",sit

应该是这样的:

s_no,id_str,screen_name
1,text1.15017060743203E+018,lorem
2,text1.15015544419693E+018,ipsum
3,text1.15015089995785E+018,dolor
4,text1.15015054311063E+018,sit

【问题讨论】:

    标签: python-3.x regex pandas csv


    【解决方案1】:
    • 使用csv 模块
    import csv
    
    with open('test.csv', 'r+', newline='') as f:
        data = list(csv.reader(f))  # produces a list of lists
        for i, r in enumerate(data):
            if i > 0:  # presumes the first list is a header and skips it
                r[1] = 'text' + r[1]  # add text to the front of the text at index 1
        f.seek(0)  # find the beginning of the file
        writer = csv.writer(f)  
        writer.writerows(data)  # write the new data back to the file
    
    # the resulting text file
    s_no,id_str,screen_name
    1,text1.15017060743203E+018,lorem
    2,text1.15015544419693E+018,ipsum
    3,text1.15015089995785E+018,dolor
    4,text1.15015054311063E+018,sit
    
    import pandas as pd
    
    # read the file set the column at index 1 as str
    df = pd.read_csv('test.csv', dtype={1: str})
    
    # add text to the column at index 1
    df.iloc[:, 1] = 'text' + df.iloc[:, 1]
    
    # write to csv
    df.to_csv('test.csv', index=False)
    
    # resulting csv
    s_no,id_str,screen_name
    1,text1.15017060743203E+018,lorem
    2,text1.15015544419693E+018,ipsum
    3,text1.15015089995785E+018,dolor
    4,text1.15015054311063E+018,sit
    

    【讨论】:

      【解决方案2】:

      这可以使用 pandas 轻松完成。

      import pandas as pd
      df = pd.read_csv("in.csv")
      df["data2"] = "text" + df["data2"].astype(str)
      df.to_csv("new_in.csv", index=False)
      

      【讨论】:

      • 谢谢,但“data2”不会对所有行都相同。我想在第 2 列中的所有数据条目前面添加“文本”。类似于遍历所有行的内容,但我不确定如何实现该编辑:非常感谢,在更改 data2 后它起作用了到column2
      • @Eagle Upvote 并接受答案,如果它对你有帮助
      • 使用df["data2"] = "text" + df["data2"].astype(str)
      • str(df["id_str"]) 会将列中的所有行转换为单个字符串,因此会搞砸
      • 你不需要给dtype,除非你想要一些特定的类型。 pandas 会尝试为你推断
      猜你喜欢
      • 2022-06-12
      • 2022-07-28
      • 2018-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      • 1970-01-01
      • 2022-01-04
      相关资源
      最近更新 更多