Skip to content
Advertisement

Tag: dataframe

Use None instead of np.nan for null values in pandas DataFrame

I have a pandas DataFrame with mixed data types. I would like to replace all null values with None (instead of default np.nan). For some reason, this appears to be nearly impossible. In reality my DataFrame is read in from a csv, but here is a simple DataFrame with mixed data types to illustrate my problem. I can’t do: nor:

how to sort pandas dataframe from one column

I have a data frame like this: As you can see, months are not in calendar order. So I created a second column to get the month number corresponding to each month (1-12). From there, how can I sort this data frame according to calendar months’ order? Answer Use sort_values to sort the df by a specific column’s values: If

Convert month int to month name in Pandas

I want to transform an integer between 1 and 12 into an abbrieviated month name. I have a df which looks like: I want the df to look like this: Most of the info I found was not in python>pandas>dataframe hence the question. Answer You can do this efficiently with combining calendar.month_abbr and df[col].apply()

pandas dataframe str.contains() AND operation

I have a df (Pandas Dataframe) with three rows: The function df.col_name.str.contains(“apple|banana”) will catch all of the rows: How do I apply AND operator to the str.contains() method, so that it only grabs strings that contain BOTH “apple” & “banana”? I’d like to grab strings that contains 10-20 different words (grape, watermelon, berry, orange, …, etc.) Answer You can do

Advertisement