大家好,我只想创建 100 个数据帧。
就像 df1、df2、df3 .. df100 一样。
如何通过 python for 循环做到这一点?
for i in range(100):
some_value = df3.loc[df3.index[i], "sentence"]
df"1" = df.loc[df['Text'] == some_value]
results = [df1, df2, ... df100]
final = pd.concat(results).drop_duplicates().reset_index(drop=True)
最佳答案
选项 1
只需填写结果而不使用变量名称:
results = []
for i in range(100):
some_value = df3.loc[df3.index[i], "sentence"]
results.append(df.loc[df['Text'] == some_value])
final = pd.concat(results).drop_duplicates().reset_index(drop=True)
选项 2
使用exec
从字符串生成变量名称(我不建议这样做):
for i in range(100):
some_value = df3.loc[df3.index[i], "sentence"]
temp_df = df.loc[df['Text'] == some_value]
exec("df{} = temp_df".format(i))
results = [df1, df2, ... df100]
final = pd.concat(results).drop_duplicates().reset_index(drop=True)
(代码可能包含错误,因为我没有编译它)
关于python - 如何通过python for循环创建100个数据框名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67469035/