LoginSignup
0
0

More than 1 year has passed since last update.

【実装メモ】pandas基礎レベル

Posted at

書くこと

久しぶりにpandasを触ったときに、実装がわからずググったことをメモる。

  • n個の要素数の Series 作成
  • DataFrameSeries を列結合( df[’name’], df.insert()
  • 既存の列aにlambda関数を実行して、結果を新規列bを作成
  • dfを2次元配列(list)に変換

最後にNotion AIの要約も載せてます。

コード

n個の要素数の Series

# %%
uuids = [str(uuid.uuid4()) for i in range(5)]
s = pd.Series(uuids)

empty_list = [None] * 5

DataFrameSeries を列結合

最後尾の列に Series 結合

# %%
df['uuid4'] = s

任意の列番号に Series結合

df.insert(0, 'top_uuid4', s)

既存a列にlambda関数、新規列bを作成

# %%
df['twice_value'] = df['value'].apply(lambda x: x * 2)

dfを2次元配列(list)に変換

values でNumPy 配列に変換。

list_from_df = df.values.tolist()

おまけ

コードサンプルの # %% はvscodeでjupyter機能を簡易に使うためのもの。

Working with Jupyter code cells in the Python Interactive window

最後に

今回出てきたレベルの加工なら、そもそもpandasを使う必要がないみたいな話があるかもしれません。このあたりの塩梅について、知見があればご意見ください!

おまけのおまけ)Notion AI 要約→翻訳

This document provides a summary of how to use the pandas library in Python, including how to create a Series with n elements, how to join Series to a DataFrame, how to create a new column in a DataFrame using a lambda function, and how to convert a DataFrame to a two-dimensional array. It also provides an additional tip on using the "# %%" code in Visual Studio Code for Jupyter support.

このドキュメントでは、n個の要素を持つSeriesの作成方法、SeriesをDataFrameに結合する方法、lambda関数を使用してDataFrameに新しい列を作成する方法、DataFrameを2次元配列に変換する方法など、Pythonのpandasライブラリの使用方法をまとめています。さらに、Visual Studio CodeでJupyterサポートを行うための「# %%」コードの使用方法についても追加で説明しています。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0