Difference between revisions of "Pandas"

From RHS Wiki
Jump to navigation Jump to search
Tag: visualeditor
Tag: visualeditor
Line 26: Line 26:
 
<syntaxhighlight lang="python3">
 
<syntaxhighlight lang="python3">
 
df.iloc[[1]]
 
df.iloc[[1]]
 +
df.loc[0]
 +
df.iloc[-1] == df.loc[df.shape[0]-1]
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Line 36: Line 38:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
=== Select multiple columns ===
+
===Select multiple columns===
 
<syntaxhighlight lang="python3">
 
<syntaxhighlight lang="python3">
 
df[['column1', 'column2', 'column3']]
 
df[['column1', 'column2', 'column3']]

Revision as of 10:04, 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]]
df.loc[0]
df.iloc[-1] == df.loc[df.shape[0]-1]

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]
# Or
df.iloc[1]['summary']