[英]How to define empty pandas DataFrame in function?
My method我的方法
def myfunc(filename, filepath):
result_df = pd.DataFrame()
with open(filename, encoding='utf-8', mode='r') as i:
data = pd.read_json(i, lines=True)
result_df.append(data)
table_from_pandas = pa.Table.from_pandas(result_df)
pq.write_table(table_from_pandas,filepath)
return result_df
Pycharm shows Pycharm 显示
(<class 'NameError'>, NameError("name 'result_df' is not defined"), <traceback object at 0x1135a0500>)
From Python shell,everything works fine.从 Python shell 开始,一切正常。 But I need to somehow define my df in advance in order to use my method.但是我需要以某种方式提前定义我的 df 才能使用我的方法。 This is my code:这是我的代码:
if __name__ == '__main__':
files = os.listdir('/Users/milenko/mario/Json_gzips')
files = [fi for fi in files if fi.endswith(".gz")]
my_dict = {'ticr_calculated_2': 'ticr-2.parquet', 'ticr_calculated_3': 'ticr-3.parquet', \
'ticr_calculated_4': 'ticr-4.parquet', 'tick_calculated_2': 'tick-2.parquet', \
'tick_calculated_3': 'tick-3.parquet', 'tick_calculated_4': 'tick-4.parquet'}
basic = '/Users/milenko/mario/Json_gzips/'
json_fi = glob.glob("*.json")
for key, value in my_dict.items():
for f in json_fi:
if re.match(key, f):
filepath = basic + value
myfunc(f, filepath)
How to solve this?如何解决这个问题?
Here is a small example for how to append data into a empty dataframe.这是一个小例子,说明如何将 append 数据转换为空的 dataframe。 You need to specify column names when defining result_df:定义result_df时需要指定列名:
import pandas as pd
def myfunc():
result_df = pd.DataFrame([], columns = ["a", "b"])
data = [5, 6]
df_length = len(result_df)
result_df.loc[df_length] = data
return result_df
print(myfunc())
Returning返回
a b
0 5 6
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.