pandas

Loading / writing data

df = pd.read_csv('data.csv', index_col=0, parse_dates=[0])

... reads ',' separated data from file using column 0 as index and parsing the same column as timestamp



DataFrame manipulations

df = df.resample('15min').ffill()

... resamples 15m and forward fills, given that df is indexed by a timestamp column



df_ticanic.sort_values(by=['Pclass', 'Age'], ascending=False, inplace=True)

... sorts df_titanic, in-place in a descending order, based on 'Pclass' and then the 'Age' columns



df = df[(df['Age'] > 45) & (df['Age'] < 48) & (df['Pclass'] == 1)]

... returns a new DataFrame that matches the above conditions



df.iloc[:, 2].fillna(0, inplace=True)

... replaces all N/A values in the 3rd column with 0



df.iloc[0:3, 3] = 'foo'

... replaces the values in the 4th column in rows 0:3 with 'foo'



df.index.is_unique

... returns True if df contains an unique index



df[df.index.duplicated(keep=False)]

... returns a new DataFrame that contains rows with duplicate index

2026-04-11 12:37:37

minicms - © 2020-2026 Simeon Simeonov