pandas_dataframe
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
pandas_dataframe [2023/05/05 21:08] – [Create a dataframe from a series of lists] admin | pandas_dataframe [2023/09/07 21:45] (current) – [lookup value] raju | ||
---|---|---|---|
Line 59: | Line 59: | ||
* https:// | * https:// | ||
+ | tags | row by row | ||
==== Create a dataframe by splitting strings ==== | ==== Create a dataframe by splitting strings ==== | ||
Given a list of strings, the idea here is to create a data frame by splitting them into multiple columns. | Given a list of strings, the idea here is to create a data frame by splitting them into multiple columns. | ||
Line 200: | Line 201: | ||
1 4 5 6 | 1 4 5 6 | ||
</ | </ | ||
+ | |||
+ | See also: | ||
+ | * https:// | ||
+ | |||
+ | ==== Create a dataframe from a bunch of variables ==== | ||
+ | < | ||
+ | import pandas as pd | ||
+ | df = pd.DataFrame({ | ||
+ | ' | ||
+ | ' | ||
+ | }) | ||
+ | </ | ||
+ | |||
+ | For example | ||
+ | < | ||
+ | $ ipython | ||
+ | |||
+ | In [1]: | ||
+ | year = 2023; month = 6; date = 15 | ||
+ | |||
+ | In [2]: | ||
+ | import pandas as pd | ||
+ | df = pd.DataFrame({ | ||
+ | ' | ||
+ | ' | ||
+ | }) | ||
+ | |||
+ | In [3]: | ||
+ | df | ||
+ | Out[3]: | ||
+ | | ||
+ | 0 | ||
+ | 1 month 6 | ||
+ | 2 | ||
+ | |||
+ | In [4]: | ||
+ | df.dtypes | ||
+ | Out[4]: | ||
+ | key object | ||
+ | value int64 | ||
+ | dtype: object | ||
+ | </ | ||
+ | |||
+ | It works even if the variables are not of the same type. | ||
+ | < | ||
+ | In [5]: | ||
+ | year = 2023; month = ' | ||
+ | |||
+ | In [6]: | ||
+ | df = pd.DataFrame({ | ||
+ | ' | ||
+ | ' | ||
+ | }) | ||
+ | |||
+ | In [7]: | ||
+ | df | ||
+ | Out[7]: | ||
+ | key value | ||
+ | 0 | ||
+ | 1 month June | ||
+ | 2 | ||
+ | |||
+ | In [8]: | ||
+ | df.dtypes | ||
+ | Out[8]: | ||
+ | key object | ||
+ | value object | ||
+ | dtype: object | ||
+ | </ | ||
+ | |||
+ | Tested with Python 3.11.3, IPython 8.12.0 | ||
+ | |||
===== selection related ===== | ===== selection related ===== | ||
==== split columns ==== | ==== split columns ==== | ||
Line 210: | Line 283: | ||
tags | uses [http:// | tags | uses [http:// | ||
+ | ==== lookup value ==== | ||
+ | To pick the first value in column ' | ||
+ | < | ||
+ | df.loc[df[' | ||
+ | </ | ||
+ | |||
+ | Example: | ||
+ | < | ||
+ | $ ipython | ||
+ | In [1]: | ||
+ | import pandas as pd | ||
+ | df = pd.DataFrame({' | ||
+ | print(df) | ||
+ | A B | ||
+ | 0 p1 1 | ||
+ | 1 p2 3 | ||
+ | 2 p3 3 | ||
+ | 3 p4 2 | ||
+ | |||
+ | In [2]: | ||
+ | df.loc[df[' | ||
+ | Out[2]: | ||
+ | 1 p2 | ||
+ | 2 p3 | ||
+ | Name: A, dtype: object | ||
+ | |||
+ | In [3]: | ||
+ | df.loc[df[' | ||
+ | Out[3]: | ||
+ | ' | ||
+ | </ | ||
+ | |||
+ | search tags | value of one column when another column equals something | ||
+ | |||
+ | Ref:- https:// | ||
===== Series related ===== | ===== Series related ===== |
pandas_dataframe.1683320889.txt.gz · Last modified: 2023/05/05 21:08 by admin