Using in operator with Pandas series [duplicate]

In the first case: Because the in operator is interpreted as a call to df[‘name’].__contains__(‘Adam’). If you look at the implementation of __contains__ in pandas.Series, you will find that it’s the following (inhereted from pandas.core.generic.NDFrame) : def __contains__(self, key): “””True if the key is in the info axis””” return key in self._info_axis so, your first … Read more

Convert dataframe with start and end date to daily data

Edit: I had to revisit this problem in a project, and looks like using DataFrame.apply with pd.date_range and DataFrame.explode is almost 3x faster: df[“date”] = df.apply( lambda row: pd.date_range(row[“start_date”], row[“end_date”]), axis=1 ) df = ( df.explode(“date”, ignore_index=True) .drop(columns=[“start_date”, “end_date”]) ) Output id age state date 0 123 18 CA 2019-02-17 1 123 18 CA 2019-02-18 … Read more

concise way of flattening multiindex columns

You can do a map join with columns out.columns = out.columns.map(‘_’.join) out Out[23]: B_mean B_std C_median A 1 0.204825 0.169408 0.926347 2 0.362184 0.404272 0.224119 3 0.533502 0.380614 0.218105 For some reason (when the column contain int) I like this way better out.columns.map(‘{0[0]}_{0[1]}’.format) Out[27]: Index([‘B_mean’, ‘B_std’, ‘C_median’], dtype=”object”)

How can I left justify text in a pandas DataFrame column in an IPython notebook

If you’re willing to use another library, tabulate will do this – $ pip install tabulate and then from tabulate import tabulate df = pd.DataFrame ({‘Text’: [‘abcdef’, ‘x’], ‘Value’: [12.34, 4.2]}) print(tabulate(df, showindex=False, headers=df.columns)) Text Value —— ——- abcdef 12.34 x 4.2 It has various other output formats also.

How can I draw scatter trend line on matplot? Python-Pandas

I’m sorry I found the answer by myself. How to add trendline in python matplotlib dot (scatter) graphs? Python import pandas as pd import numpy as np import matplotlib.pyplot as plt csv = pd.read_csv(‘/tmp/test.csv’) data = csv[[‘fee’, ‘time’]] x = data[‘fee’] y = data[‘time’] plt.scatter(x, y) z = np.polyfit(x, y, 1) p = np.poly1d(z) plt.plot(x,p(x),”r–“) … Read more