Is it possible to append Series to rows of DataFrame without making a list first?

Maybe an easier way would be to add the pandas.Series into the pandas.DataFrame with ignore_index=True argument to DataFrame.append(). Example – DF = DataFrame() for sample,data in D_sample_data.items(): SR_row = pd.Series(data.D_key_value) DF = DF.append(SR_row,ignore_index=True) Demo – In [1]: import pandas as pd In [2]: df = pd.DataFrame([[1,2],[3,4]],columns=[‘A’,’B’]) In [3]: df Out[3]: A B 0 1 2 … Read more

Is there a simple way to change a column of yes/no to 1/0 in a Pandas dataframe?

method 1 sample.housing.eq(‘yes’).mul(1) method 2 pd.Series(np.where(sample.housing.values == ‘yes’, 1, 0), sample.index) method 3 sample.housing.map(dict(yes=1, no=0)) method 4 pd.Series(map(lambda x: dict(yes=1, no=0)[x], sample.housing.values.tolist()), sample.index) method 5 pd.Series(np.searchsorted([‘no’, ‘yes’], sample.housing.values), sample.index) All yield 0 0 1 0 2 1 3 0 4 0 5 0 6 0 7 0 8 1 9 1 timing given sample timing … Read more

Getting a list of indices where pandas boolean series is True

Using Boolean Indexing >>> s = pd.Series([True, False, True, True, False, False, False, True]) >>> s[s].index Int64Index([0, 2, 3, 7], dtype=”int64″) If need a np.array object, get the .values >>> s[s].index.values array([0, 2, 3, 7]) Using np.nonzero >>> np.nonzero(s) (array([0, 2, 3, 7]),) Using np.flatnonzero >>> np.flatnonzero(s) array([0, 2, 3, 7]) Using np.where >>> np.where(s)[0] … Read more

Finding the intersection between two series in Pandas

Place both series in Python’s set container then use the set intersection method: s1.intersection(s2) and then transform back to list if needed. Just noticed pandas in the tag. Can translate back to that: pd.Series(list(set(s1).intersection(set(s2)))) From comments I have changed this to a more Pythonic expression, which is shorter and easier to read: Series(list(set(s1) & set(s2))) … Read more

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