I’m trying to save some data that I collected on a csv file.
And for that I’m using the following code, but I’m getting the error:
‘str’ object has no attribute ‘to_csv’
I am using this line df = pd.to_numeric(df, errors=’ignore’) to change Nonetype to numeric type. is this the correct method?
import pandas as pd
import numpy as np
df = pd.read_csv("D:data_ana\2022.07.27_at_10.00.33.csv")
index = 5
cols_in_the_slice = df.loc[
    :,
    (
        f"Objects[{index}].General.u_MeasuredTimeStamp",
        f"Objects[{index}].General.u_LifeCycles",
        f"Objects[{index}].KinematicRel.f_DistX",
        f"Objects[{index}].KinematicRel.f_DistY",
        f"Objects[{index}].KinematicRel.f_VrelX",
    ),
].columns
other_cols = pd.Index(["TimeStamp", "Velocity", "Accel", "YawRate"])
all_cols = other_cols.union(cols_in_the_slice, sort=False)
df = df[all_cols]
df.rename(
    columns={
        f"Objects[{index}].General.u_MeasuredTimeStamp": "Obj_TimeStamp",
        f"Objects[{index}].General.u_LifeCycles": "Age",
        f"Objects[{index}].KinematicRel.f_DistX": "K_DistX",
        f"Objects[{index}].KinematicRel.f_DistY": "K_DistY",
        f"Objects[{index}].KinematicRel.f_VrelX": "K_VrelX",
        f"Objects[{index}].KinematicRel.f_VrelY": "K_VrelY",
    },
    inplace=True,
)
df = str(round(df, 2))
df = pd.to_numeric(df, errors="ignore")
df = df.to_csv(r"D:data_anaduplicate_2022.07.27_at_10.00.33.csv")
Advertisement
Answer
The issue isn’t (only) in pd.to_numeric(); right before that you’re str()ing the df and assigning to df, so at that point you have no dataframe left, just a string describing it.
Additionally, you can’t use to_numeric like that.
If you want to convert everything in the df to numbers, you can use astype.
Furthermore, to_csv doesn’t return the dataframe.
df.rename(...) df.astype(float, copy=False, errors='ignore') df.to_csv(r'D:data_anaduplicate_2022.07.27_at_10.00.33.csv')
You can also simplify things by just telling to_csv which columns to write:
import pandas as pd
df = pd.read_csv(r"D:data_ana2022.07.27_at_10.00.33.csv")
index = 5
df.rename(
    columns={
        f"Objects[{index}].General.u_MeasuredTimeStamp": "Obj_TimeStamp",
        f"Objects[{index}].General.u_LifeCycles": "Age",
        f"Objects[{index}].KinematicRel.f_DistX": "K_DistX",
        f"Objects[{index}].KinematicRel.f_DistY": "K_DistY",
        f"Objects[{index}].KinematicRel.f_VrelX": "K_VrelX",
        f"Objects[{index}].KinematicRel.f_VrelY": "K_VrelY",
    },
    inplace=True,
)
df.astype(float, copy=False, errors="ignore")
cols_to_write = [
    "TimeStamp",
    "Velocity",
    "Accel",
    "YawRate",
    "Obj_TimeStamp",
    "Age",
    "K_DistX",
    "K_DistY",
    "K_VrelX",
    "K_VrelY",
]
df = df.to_csv(r"D:data_anaduplicate_2022.07.27_at_10.00.33.csv", columns=cols_to_write)
