How are iloc and loc different?

Label vs. Location The main distinction between the two methods is: loc gets rows (and/or columns) with particular labels. iloc gets rows (and/or columns) at integer locations. To demonstrate, consider a series s of characters with a non-monotonic integer index: >>> s = pd.Series(list(“abcdef”), index=[49, 48, 47, 0, 1, 2]) 49 a 48 b 47 … Read more

Combine two columns of text in pandas dataframe

If both columns are strings, you can concatenate them directly: df[“period”] = df[“Year”] + df[“quarter”] If one (or both) of the columns are not string typed, you should convert it (them) first, df[“period”] = df[“Year”].astype(str) + df[“quarter”] Beware of NaNs when doing this! If you need to join multiple string columns, you can use agg: … Read more

Drop data frame columns by name

You can use a simple list of names : DF <- data.frame( x=1:10, y=10:1, z=rep(5,10), a=11:20 ) drops <- c(“x”,”z”) DF[ , !(names(DF) %in% drops)] Or, alternatively, you can make a list of those to keep and refer to them by name : keeps <- c(“y”, “a”) DF[keeps] EDIT : For those still not acquainted … Read more

Remove rows with all or some NAs (missing values) in data.frame

Also check complete.cases : > final[complete.cases(final), ] gene hsap mmul mmus rnor cfam 2 ENSG00000199674 0 2 2 2 2 6 ENSG00000221312 0 1 2 3 2 na.omit is nicer for just removing all NA‘s. complete.cases allows partial selection by including only certain columns of the dataframe: > final[complete.cases(final[ , 5:6]),] gene hsap mmul mmus … Read more

Pretty-print an entire Pandas Series / DataFrame

You can also use the option_context, with one or more options: with pd.option_context(‘display.max_rows’, None, ‘display.max_columns’, None): # more options can be specified also print(df) This will automatically return the options to their previous values. If you are working on jupyter-notebook, using display(df) instead of print(df) will use jupyter rich display logic (like so).

How to deal with SettingWithCopyWarning in Pandas

The SettingWithCopyWarning was created to flag potentially confusing “chained” assignments, such as the following, which does not always work as expected, particularly when the first selection returns a copy. [see GH5390 and GH5597 for background discussion.] df[df[‘A’] > 2][‘B’] = new_val # new_val not set in df The warning offers a suggestion to rewrite as … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)