pandas_dataframe
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
pandas_dataframe [2023/05/05 21:06] – [Create a dataframe from a series of lists] admin | pandas_dataframe [2023/07/21 22:16] – [Create a dataframe from list of lists] 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 173: | Line 174: | ||
</ | </ | ||
+ | But with | ||
< | < | ||
In [10]: | In [10]: | ||
Line 199: | 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 ==== |
pandas_dataframe.txt · Last modified: 2024/05/22 15:47 by admin