【发布时间】:2021-11-27 16:27:57
【问题描述】:
我有多个具有相同列的 dfs。这是所有dfs的列表
dfs = [df_14, df_15, df_16, df_17]
例如,每个数据帧看起来像这样,df_14:
| id | Days |
|---|---|
| 001 | 0 |
| 004 | 56 |
| 013 | 95 |
| 015 | 33 |
接下来,df_15:
| Id | Days |
|---|---|
| 001 | 0 |
| 023 | 18 |
| 459 | 19 |
| 811 | 35 |
df_16:
| Id | Days |
|---|---|
| 111 | 93 |
| 114 | 56 |
| 232 | 0 |
df_17:
| Id | Days |
|---|---|
| 532 | 120 |
| 113 | 31 |
| 065 | 58 |
| 015 | 2 |
我的代码:
rows = [['532', 120],['113', 31], ['065', 58],['025', 2]]
for row in rows:
df_14.loc[len(df_14)] = row
# and so on
任务是附加到每个月的列表 - 有 30-60 天的列表和另一个单独的列表,其中包含 60-100 天的客户 ID。
#The result should be like this:
14_1: ['004', '015']
14_2: ['013']
15_1: ['811']
我尝试在上面使用 f'strings。比如:
abrreviations = ['14', '15','16', '17']
c = ['_1', '_2']
#Have wrote initializing loops like
m_list=[]
for a in abrreviations:
for cp in c:
m_list.append(a+cp)
这个想法是在带有 f'string 或格式的循环中使用缩写。但是不知道怎么做?或者您能提供其他想法吗?
【问题讨论】:
标签: python pandas list dataframe loops