So I got a CSV file that has 14 columns, and I was trying to convert that data to string type and I tried this
import pandas as pd
df = pd.read_csv("2008_data_test.csv", sep=",")
output = pd.DataFrame(columns=df.columns)
for c in df.columns:
if df[c].dtype == object:
print "convert ", df[c].name, " to string"
df[c] = df[c].astype(str)
output.to_csv("2008_data_test.csv_tostring2.csv", index=False)
This gives me the headers only, and I can't figure out what I missed?
Any ideas? And is it possible to convert specific columns?
output = pd.DataFrame(columns=df.columns)is defined before theforloop. That's what you write to CSV; theforloop does basically nothing. Did you meandf.to_csv("2008_data_test.csv_tostring2.csv", index=False)instead?