How to check if a pandas dataframe contains only numeric values column-wise?

You can check that using to_numeric and coercing errors: pd.to_numeric(df[‘column’], errors=”coerce”).notnull().all() For all columns, you can iterate through columns or just use apply df.apply(lambda s: pd.to_numeric(s, errors=”coerce”).notnull().all()) E.g. df = pd.DataFrame({‘col’ : [1,2, 10, np.nan, ‘a’], ‘col2’: [‘a’, 10, 30, 40 ,50], ‘col3’: [1,2,3,4,5.0]}) Outputs col False col2 False col3 True dtype: bool

How to convert single-row pandas data frame to series?

You can transpose the single-row dataframe (which still results in a dataframe) and then squeeze the results into a series (the inverse of to_frame). df = pd.DataFrame([list(range(5))], columns=[“a{}”.format(i) for i in range(5)]) >>> df.squeeze(axis=0) a0 0 a1 1 a2 2 a3 3 a4 4 Name: 0, dtype: int64 Note: To accommodate the point raised by … Read more

Why use loc in Pandas?

Explicit is better than implicit. df[boolean_mask] selects rows where boolean_mask is True, but there is a corner case when you might not want it to: when df has boolean-valued column labels: In [229]: df = pd.DataFrame({True:[1,2,3],False:[3,4,5]}); df Out[229]: False True 0 3 1 1 4 2 2 5 3 You might want to use df[[True]] … Read more

How to get maximum length of each column in the data frame using pandas python

One solution is to use numpy.vectorize. This may be more efficient than pandas-based solutions. You can use pd.DataFrame.select_dtypes to select object columns. import pandas as pd import numpy as np df = pd.DataFrame({‘A’: [‘abc’, ‘de’, ‘abcd’], ‘B’: [‘a’, ‘abcde’, ‘abc’], ‘C’: [1, 2.5, 1.5]}) measurer = np.vectorize(len) Max length for all columns res1 = measurer(df.values.astype(str)).max(axis=0) … Read more

Python Reindex Producing Nan

The docs are clear on this behaviour : Conform Series to new index with optional filling logic, placing NA/NaN in locations having no value in the previous index if you just want to overwrite the index values then do: In [32]: test3.index = [‘f’,’g’,’z’] test3 Out[32]: f 1 g 2 z 3 dtype: int64