Reputation: 411
I am not able to assign NA values to dataframe and while exporting to_csv file,its shows empty. please suggest.
Looking out put of Data_1 is :
for i in range(1, length):
Nnw_ = np.array(data_1.iloc[0:(data_1.iloc[0:, i]).count(),[i]])
rep = np.array(data_2.iloc[0:( data_2.iloc[0:, i]).count(),[i + 1]])
data_1.replace(Nnw, rep, inplace=True)
Upvotes: 0
Views: 6788
Reputation: 30971
If you read a DataFrame from a CSV file, it may contain missing values represented a.o. as NA. So the source file can contain e.g.:
data_1,data_2
red,Rose
white,NA
yelow,NA
orange,Orange
But read_csv converts such missing values to np.nan, which are printed as NaN and both columns are of object type.
As of Pandas version 1.0 it is possible to convert such columns to string type:
df = df.convert_dtypes()
Now, when you run df.info()
, you will get:
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 4 entries, 0 to 3
Data columns (total 2 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 data_1 4 non-null string
1 data_2 2 non-null string
dtypes: string(2)
memory usage: 192.0 bytes
(compare with the result of df.info()
just after read_csv,
where the type is object).
This DataFrame can be saved to a CSV file, running e.g.
df.to_csv('Output.csv', index=False, na_rep='NA')
and the result will be:
data_1,data_2
red,Rose
white,NA
yelow,NA
orange,Orange
Note parameters:
index=False
- to omit the output of the index,na_rep='NA'
- to provide a visible representation of NA values
(the default representation is an empty string).If you want to replace individual cells with NA, run e.g.:
df.iloc[3,1] = np.nan
Don't worry that you used np.nan. Pandas is clever enough to convert it into NA.
If you copy values from data_2 to data_1, there is no problem. Note that after convert_dtypes both columns area of string type, so NA values can be copied also to data_1.
If you don't want the missing values saved as NA, just drop na_rep parameter from to_csv and they will be save as (the default) empty strings.
Upvotes: 1