如何把dataframe里的表格读取成csv文件


import pandas as pd
train_df = pd.DataFrame(train_samples)
train_df.head(14)

target    text    line_number    total_lines
0    OBJECTIVE    to investigate the efficacy of @ weeks of dail...    0    11
1    METHODS    a total of @ patients with primary knee oa wer...    1    11
2    METHODS    outcome measures included pain reduction and i...    2    11
3    METHODS    pain was assessed using the visual analog pain...    3    11
4    METHODS    secondary outcome measures included the wester...    4    11
5    METHODS    serum levels of interleukin @ ( il-@ ) , il-@ ...    5    11
6    RESULTS    there was a clinically relevant reduction in t...    6    11
7    RESULTS    the mean difference between treatment arms ( @...    7    11
8    RESULTS    further , there was a clinically relevant redu...    8    11
9    RESULTS    these differences remained significant at @ we...    9    11
10    RESULTS    the outcome measures in rheumatology clinical ...    10    11
11    CONCLUSIONS    low-dose oral prednisolone had both a short-te...    11    11
12    BACKGROUND    emotional eating is associated with overeating...    0    10
13    BACKGROUND    yet , empirical evidence for individual ( trai...    1    10


用dataframe打开以后,是以下这样的表格,想提取为csv文件,用了一些dataframe转csv的代码,出现了错误:


TypeError: argument of type 'method' is not iterable

请教大家该如何解决?

错误提示是方法类型的参数不可迭代。如果循环写入csv的话,可这样:
for i,df in enumerate(dfs):
df.to_csv(f'df{i}.csv',index=False)

发一下你转换的代码

这条语句出错,还是什么语句出错?

train_df.to_csv("data.csv")

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632