Difference between revisions of "Pandas"

From RHS Wiki
Jump to navigation Jump to search
Tag: visualeditor
Tag: visualeditor
Line 6: Line 6:
 
==Read CSV==
 
==Read CSV==
 
<syntaxhighlight lang="python3">
 
<syntaxhighlight lang="python3">
news = pd.read_csv('news_2019.05.10.csv')
+
df = pd.read_csv('news_2019.05.10.csv')
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
==Dataframe==
 
==Dataframe==
  
=== Info ===
+
===Info===
 
<syntaxhighlight lang="python3">
 
<syntaxhighlight lang="python3">
news.index
+
df.index
news.columns
+
df.columns
news.values
+
df.values
 +
df.head()
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
===Select 1 row===
 
===Select 1 row===
 
<syntaxhighlight lang="python3">
 
<syntaxhighlight lang="python3">
texts.iloc[[1]]
+
df.iloc[[1]]
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
===Select 1 column===
 
===Select 1 column===
 
<syntaxhighlight lang="python3">
 
<syntaxhighlight lang="python3">
sumarys = news[['summary']]
+
sumarys = df[['summary']]
 
# Or
 
# Or
 
list(df['one'])
 
list(df['one'])
Line 33: Line 34:
 
===Select 1 cell===
 
===Select 1 cell===
 
<syntaxhighlight lang="python3">
 
<syntaxhighlight lang="python3">
texts.iloc[1][1]
+
df.iloc[1][1]
 
# Or
 
# Or
texts.iloc[1]['summary']
+
df.iloc[1]['summary']
 
</syntaxhighlight>
 
</syntaxhighlight>
 
[[Category:Python]]
 
[[Category:Python]]

Revision as of 06:54, 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.head()

Select 1 row

df.iloc[[1]]

Select 1 column

sumarys = df[['summary']]
# Or
list(df['one'])
dfToList = df['one'].tolist()

Select 1 cell

df.iloc[1][1]
# Or
df.iloc[1]['summary']