如何在 pandas 数据帧变量上使用 format()
How to use format() on pandas dataframe variable
我有以下 pandas 个数据帧
phreatic_level_l2n1_28w_df.head()
Fecha Hora PORVL2N1 # PORVLxNx column change their name in each data frame
0 2012-01-12 01:37:47 0.65
1 2012-01-12 02:37:45 0.65
2 2012-01-12 03:37:50 0.64
3 2012-01-12 04:37:44 0.63
4 2012-01-12 05:37:45 0.61
如此,依次直到有25个phreatic_level_l24n2_28w_df
类型的数据帧
.
.
.
phreatic_level_l24n2_28w_df.head()
Fecha Hora PORVL24N2 # PORVLxNx column change their name in each data frame
0 2018-01-12 01:07:28 1.31
1 2018-01-12 02:07:28 1.31
2 2018-01-12 03:07:29 1.31
3 2018-01-12 04:07:27 1.31
4 2018-01-12 05:07:27 1.31
我的objective是迭代每条记录(所有数据帧)应用下面的过程
for i in range(1,25):
if (i==2):
# We turn to datetime the Fecha column values
phreatic_level_l{}n{}_28w_df['Fecha'].format(i,i-1) = pd.to_datetime(phreatic_level_l'{}'n'{}'_28w_df['Fecha'].format(i,i-1))
.
.
# And so, successively until have 25 data frames
但是我有以下错误,由于 format()
函数,它应该只应用于字符串而不是任何变量名。
File "<ipython-input-72-1f6ad7811399>", line 5
phreatic_level_l{}n{}_28w_df['Fecha'].format(i,i-1) = pd.to_datetime(phreatic_level_l'{}'n'{}'_28w_df['Fecha'].format(i,i-1))
^
SyntaxError: invalid syntax
str.format
适用于字符串。您正在尝试在变量名称上使用它。
您可以将 DataFrame
放在 dict
中,然后通过字符串引用它们。
dfs = {
'phreatic_level_l1n0_28w_df': phreatic_level_l1n0_28w_df,
'phreatic_level_l2n1_28w_df': phreatic_level_l1n0_28w_df,
'phreatic_level_l3n2_28w_df': phreatic_level_l1n0_28w_df,
...
}
for name, df in dfs.items():
df = pd.to_datetime(df['Fecha'])
您还可以像这样访问特定的 DataFrames
dfs['phreatic_level_l3n2_28w_df']
。
或者,您可以将它们存储在 list
中并迭代它们
dfs = [
phreatic_level_l1n0_28w_df,
phreatic_level_l2n1_28w_df,
phreatic_level_l3n2_28w_df,
...
]
for df in dfs:
df = pd.to_datetime(df['Fecha'])
如果您按照变量名的顺序存储它们,您可以以更简洁的方式访问它们,即 dfs[0]
.
最后,查看 this 是关于 str.format
的精彩教程
我有以下 pandas 个数据帧
phreatic_level_l2n1_28w_df.head()
Fecha Hora PORVL2N1 # PORVLxNx column change their name in each data frame
0 2012-01-12 01:37:47 0.65
1 2012-01-12 02:37:45 0.65
2 2012-01-12 03:37:50 0.64
3 2012-01-12 04:37:44 0.63
4 2012-01-12 05:37:45 0.61
如此,依次直到有25个phreatic_level_l24n2_28w_df
.
.
.
phreatic_level_l24n2_28w_df.head()
Fecha Hora PORVL24N2 # PORVLxNx column change their name in each data frame
0 2018-01-12 01:07:28 1.31
1 2018-01-12 02:07:28 1.31
2 2018-01-12 03:07:29 1.31
3 2018-01-12 04:07:27 1.31
4 2018-01-12 05:07:27 1.31
我的objective是迭代每条记录(所有数据帧)应用下面的过程
for i in range(1,25):
if (i==2):
# We turn to datetime the Fecha column values
phreatic_level_l{}n{}_28w_df['Fecha'].format(i,i-1) = pd.to_datetime(phreatic_level_l'{}'n'{}'_28w_df['Fecha'].format(i,i-1))
.
.
# And so, successively until have 25 data frames
但是我有以下错误,由于 format()
函数,它应该只应用于字符串而不是任何变量名。
File "<ipython-input-72-1f6ad7811399>", line 5
phreatic_level_l{}n{}_28w_df['Fecha'].format(i,i-1) = pd.to_datetime(phreatic_level_l'{}'n'{}'_28w_df['Fecha'].format(i,i-1))
^
SyntaxError: invalid syntax
str.format
适用于字符串。您正在尝试在变量名称上使用它。
您可以将 DataFrame
放在 dict
中,然后通过字符串引用它们。
dfs = {
'phreatic_level_l1n0_28w_df': phreatic_level_l1n0_28w_df,
'phreatic_level_l2n1_28w_df': phreatic_level_l1n0_28w_df,
'phreatic_level_l3n2_28w_df': phreatic_level_l1n0_28w_df,
...
}
for name, df in dfs.items():
df = pd.to_datetime(df['Fecha'])
您还可以像这样访问特定的 DataFrames
dfs['phreatic_level_l3n2_28w_df']
。
或者,您可以将它们存储在 list
中并迭代它们
dfs = [
phreatic_level_l1n0_28w_df,
phreatic_level_l2n1_28w_df,
phreatic_level_l3n2_28w_df,
...
]
for df in dfs:
df = pd.to_datetime(df['Fecha'])
如果您按照变量名的顺序存储它们,您可以以更简洁的方式访问它们,即 dfs[0]
.
最后,查看 this 是关于 str.format