Pandas Cheat Sheet Extra text to meet the character requirements
|
df = pd.read_csv('filename.csv')
|
Read CSV into a Pandas DataFrame
|
|
df = pd.to_csv('filename.csv')
|
Export Pandas DataFrame to CSV
|
|
all_files = glob.glob(*/.txt')
|
Finds all txt files in the
|
|
df_raw = [pd.read_csv(f) for f in all_files]
|
Makes multiple DataFrames
|
|
df_all = pd.concat(df_raw, ignore_index=True
|
Concatenates all the DataFrames into one (1) large DataFrame
|
|
df.head(5)
|
Reads the first 5 rows
|
|
df.tail(5)
|
Reads the last 5 rows
|
|
df.shape()
|
Gives the number of columns and rows in the DataFrame
|
|
data.iloc[0]
|
First row of DataFrame
|
|
data.iloc[1]
|
Second row of DataFrame
|
|
data.iloc[:0]
|
First column of DataFrame
|
|
data.iloc[:1]
|
Second column of DataFrame
|
|
df.iloc[:3, :2]
|
Selecting first 3 rows and first 2 columns
|
|
df.iloc[:3, ['Column1', 'Column2']]
|
Selecting first 3 rows and first 2 columns
|
|
df = df[['Column3', 'Column2', 'Column1']]
|
Re-orders the columns to the order specified in this list
|
|
df= df.drop(columns=['Column1'], axis=1)
|
Drops 'Column1_Name' from DataFrame
|
|
df = df.sort_values(by=['Column1'], ascending=False)
|
Sort values by Column1
|
|
df= df[(df['Column1'] >= some_number]
|
Filter DataFrame by certain value
|
|
df.columns = ['A', 'B']
|
Renamed Columns to 'A' and 'B'
|
|
df1.append(df2)
|
Joins df1 and df2
|
|
df = df[(df> 2).all(axis=1)]
|
Removes any values less than 2
|
|
row = df[df.A > 3].iloc[0]
|
Select first row where A > 0
|
|
df= df.fillna(method='ffill')
|
Fills blank values using forward fill method
|
|
df= df.fillna(method='bfill')
|
Fills blank values using backwards fill method
|
|
df.dropna(inplace=True)
|
Removes rows with no values
|
|
df['Column1'] = df['Column1'].astype(str).str.zfill(6)
|
Sets the numbe to six (6) long, which adds zeros
|
|
df['Column1']= df['Column1'].astype(float)
|
Change 'Column1' to float
|
|
df['Time'] = df['Time'].apply(pd.to_datetime)
|
Converts the time column to a Datetime Series
|
No comments yet.
Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.