Difference between revisions of "Pandas"
Jump to navigation
Jump to search
Rafahsolis (talk | contribs) m (→Select 1 row) Tag: visualeditor |
Rafahsolis (talk | contribs) m (→Select 1 row) Tag: visualeditor |
||
| Line 25: | Line 25: | ||
===Select 1 row=== | ===Select 1 row=== | ||
<syntaxhighlight lang="python3"> | <syntaxhighlight lang="python3"> | ||
| − | df.iloc[[1]] | + | df.iloc[[1]] # for positional indexing |
| − | df.loc[0] | + | df.loc[0] # for label based |
df.iloc[-1] == df.loc[df.shape[0]-1] | df.iloc[-1] == df.loc[df.shape[0]-1] | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | === Select specific rows === | ||
| + | <syntaxhighlight lang="python3"> | ||
| + | df.loc[[9, 99, 999]] | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| Line 46: | Line 51: | ||
<syntaxhighlight lang="python3"> | <syntaxhighlight lang="python3"> | ||
df.iloc[1][1] | df.iloc[1][1] | ||
| − | |||
df.iloc[1]['summary'] | df.iloc[1]['summary'] | ||
| + | df.iloc[1, 3] | ||
| + | |||
| + | df.loc[1, 'summary'] | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | === Subset multiple rows and multiple columns === | ||
| + | <syntaxhighlight lang="python3"> | ||
| + | df.iloc[[1,34,56],[2,4,5]] | ||
| + | df.loc[[1,34,56],['modification_date', 'content']] | ||
</syntaxhighlight> | </syntaxhighlight> | ||
[[Category:Python]] | [[Category:Python]] | ||
Revision as of 10:17, 21 May 2019
Install
pip install pandas
Read CSV
df = pd.read_csv('news_2019.05.10.csv')
Dataframe
Info
df.index
df.columns
df.values
df.shape
df.dtypes
df.head()
df.tail()
df.info()
Select 1 row
df.iloc[[1]] # for positional indexing
df.loc[0] # for label based
df.iloc[-1] == df.loc[df.shape[0]-1]
Select specific rows
df.loc[[9, 99, 999]]
Select 1 column
sumarys = df[['summary']]
# Or
list(df['one'])
dfToList = df['one'].tolist()
Select multiple columns
df[['column1', 'column2', 'column3']]
Select 1 cell
df.iloc[1][1]
df.iloc[1]['summary']
df.iloc[1, 3]
df.loc[1, 'summary']
Subset multiple rows and multiple columns
df.iloc[[1,34,56],[2,4,5]]
df.loc[[1,34,56],['modification_date', 'content']]