我正在尝试使用另一个df(receiver_df
)中的某些列在一个df(send_df
)中设置一些值。
使用:
receiver_df.loc[send_df.index, stat_clmns_lst] = send_df.iloc[0][stat_col_lst]
代码默默无效。 send_df.index
似乎是个问题。
此过程是循环的一部分。所以.join
不起作用。在第一遍中,我不知道send_df
将要通过的所有列。以下是第一次传递数据的方式。
import numpy as np
import pandas as pd
import string
from IPython.display import display
# Parameters
send_df_index = 2 # Tells send_df where to send data in receiver df
num_rows = 5
# Columns to get from send_df
send_col_lst = ['Some_Index', 'Another_Index']
# Datarame receiving data
receiver_df = pd.DataFrame({'Attempted':[False for x in range(num_rows)],
'StringX':[pd.util.testing.rands(3) for x in range(num_rows)],
'StringY':[pd.util.testing.rands(3) for x in range(num_rows)]
})
# Dataframe sending data
send_values = random.choices(string.ascii_uppercase, k=1)
send_values.extend(np.random.uniform(low=2, high=100, size=(1,2)).tolist()[0])
send_df = pd.DataFrame([send_values],
columns=['String', 'Some_Index', 'Another_Index'],
index=[stats_df_index])
# Reindex - We dont know the stat columns ahead of time
new_col_lst = receiver_df.columns.values.tolist()
new_col_lst.extend(send_col_lst)
receiver_df = receiver_df.reindex(columns=new_col_lst)
# Set values to receiving df send_df[stat_col_lst]
receiver_df.loc[send_df.index, send_col_lst] = send_df.iloc[0][send_col_lst]
display(receiver_df)
答案 0 :(得分:0)
我不知道如何在轴1(列)上进行连接以在迭代中工作。另一种解决方法是附加你的"发送df"在轴0上迭代,然后在for循环之后将它连接到接收器df
sending_df = pd.DataFrame()
for i in range(k):
sending_df = sending_df.append(df2)
receiver_df = receiver_df.join(sending_df)