對 python 進行資料分析相當新,仍然是一個菜鳥。
我有一個熊貓資料框串列( 100),它們的變數保存在一個串列中。
然后我將變數以字串格式保存在另一個串列中,以在繪圖時作為識別符號添加到資料幀中。
我已經定義了一個函式來為以后的特征工程準備表格。
我想遍歷每個資料框并將相應的字串添加到名為“Strings”的列中
df = [df1, df2, df3]
strings = ['df1', 'df2', 'df3']
def mindex(df):
# remove time index and insert Strings column
df.reset_index(inplace=True)
df.insert(1, "Strings", "")
# iterate through each table adding the string values
for item in enumerate(df):
for item2 in strings:
df['Strings'] = item2
# the loop to cycle through all the dateframes using the function above
for i in df:
mindex(i)
當我使用上面的函式時,它只會將最后一個值填充到所有資料幀中。我想指出所有資料框都在相同的日期范圍內,因為我試圖使用它作為一種方法來停止迭代而沒有獲勝。
誰能指出我正確的方向!到目前為止,谷歌還不是我的朋友
uj5u.com熱心網友回復:
df = [df1, df2, df3]
strings = ['df1', 'df2', 'df3']
for s, d in zip(strings, df):
d['Strings'] = s
uj5u.com熱心網友回復:
在行中,df['Strings'] = item2您將變數 item2 分配到整個列 df["Strings"]。所以第一次迭代分配“df1”,第二次分配“df2”并以“df3”結束,這就是你最終看到的。
如果你想在列字串中完全填充 df1 的“df1”,df2 的“df2”等等,你必須:
def mindex(dfs: list, strings: list) -> list:
final_dfs = []
for single_df, df_name in zip(dfs, strings):
single_df = single_df.copy()
single_df.reset_index(inplace=True)
single_df.insert(1, "Strings", "")
single_df['Strings'] = df_name
final_dfs.append(single_df)
return final_dfs
dfs = [df1, df2, df3]
strings = ['df1', 'df2', 'df3']
result = mindex(dfs, strings)
幾個要點:
- 如果您定義 dfs 串列,請將其命名為 dfs(復數),而不是 df。
dfs = [df1, df2, df3]
- 如果您遍歷 pandas DataFrame,請使用 df.iterrows()。它將生成索引和行,因此您無需應用enumerate。
for idx, row in df.iterrows():
....
- 如果您在 for 回圈中使用不會被使用的變數,就像在您的示例item 中一樣,請改用下劃線。對于無用的變數,這是一種很好的做法:
for _ in enumerate(df):
for item2 in strings:
df['Strings'] = item2
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/332701.html
上一篇:在撲克牌串列中尋找對
下一篇:串列串列中的重復項
