Christina Zhou
Christina Zhou

Reputation: 1863

Read all columns in as string in pandas

I want to read the entire dataframe as string.

The # of columns changes sometimes in the dataframe I pass, so I don't want to hardcode which columns specifically to read in.

The problems I'm dealing with are (a) lots of NaNs considered as floats when I want to consider as strings in many columns, (b) a person's last name, NA, getting read in as literally not a value:

     Unnamed: 0_level_0 Unnamed: 1_level_0
     Unnamed: 0_level_1 Unnamed: 1_level_1
              Last Name         First Name
4940                Lee              Harry
4941               Chin            Vincent
4942                NaN             Doreen

Upvotes: 2

Views: 5759

Answers (1)

Simon Osipov
Simon Osipov

Reputation: 424

If we are talking about reading *.csv, then consider using dtype=str as one of the parameters for pd.read_csv. Check documentation

If you already have a dataframe, you should use df.to_string method. Here is documentation

Upvotes: 4

Related Questions