-3

In csv files numeric values are converted to string format. When I try to upload such files via postgres, system doesn't recognize these numeric columns.

I have tried to modify the data type of such columns within the csv file using python using

df=pd.read_csv(r"C:\My Files\data1.csv")
df.dtypes

df[14]=pd.to_numeric("PRICE",errors='coerce')

df.dtypes

However I get the error noted below:

DtypeWarning: Columns (1,2,7) have mixed types. Specify dtype option on import or set low_memory=False.
  interactivity=interactivity, compiler=compiler, result=result

Data type of this column continues to remain as OBJECT in Python.

Pls help.

tdelaney
  • 73,364
  • 6
  • 83
  • 116
SRJ
  • 1
  • [how to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – Mark Tolonen Apr 01 '23 at 16:00
  • This question lacks the full stack trace and a reasonable sample of data. CSV is a string format and csv readers need to do their own conversion to other types. Maybe data types are specified or maybe the tool tries to guess the right one. Since we don't have the stack trace, we don't know if the problem is in the csv reader or somewhere else. And since we don't have data, no help there either. – tdelaney Apr 02 '23 at 21:56

1 Answers1

0

I am unclear what df[14] represents. In any case, to convert just one column you can use:

df['Price'] = pd.to_numeric(df['Price'], errors = 'coerce')

to convert a column using numerical indexing you can refer to is as :

df.iloc[:,14]

otherwise to convert one or more columns you can use:

mycols = ['col1', 'col2']

df[mycols] = df[mycols].apply(pd.to_numeric, errors='coerce', axis=1)
user19077881
  • 3,643
  • 2
  • 3
  • 14
  • Thank you. df[14] was basically to convert the 14h column in the dataframe. – SRJ Apr 05 '23 at 10:47
  • However, the issue still remains. – SRJ Apr 06 '23 at 04:24
  • See edited answer. `df[14]` does not refer to the 14th column so you need to use `iloc`. `df[14]` refers to a column with an index **label** 14. Note that column indexing starts from 0 so `df.iloc[:,14]` would be the 15th column. Note that `errors = 'coerce'` will change nonconvertible values to NaN; `='ignore'` will leave such values as-is. – user19077881 Apr 06 '23 at 14:44