我正在研究一个熊猫数据框,该数据框包含3列,分别为:drugName,review和rating。 我正在尝试根据其比率(如果该比率大于或等于6)进行审查,因此我必须将其写入csv文件中,这是一项积极的审查。这是我的代码:
import csv
import pandas as pd
filename ="C:\\Users\Amin Chaari\Desktop\Book1.csv"
def user_text(filename):
with open (filename, encoding="utf8") as f:
datas = csv.reader(f, delimiter = ';')
lines = [row for row in datas]
user={}
try:
for i in range(1,5):
if lines[0][i] != 'condition':
print(lines[0][i])
grouped_column = []
for j,row in enumerate(lines):
if j>0:
grouped_column.append(row[i])
user.update({lines[0][i]:grouped_column})
except IndexError:
pass
df1=pd.DataFrame(user)
df1.groupby(['review'])
return df1
df=user_text(filename)
for i in range (0,40303):
df['rating'][i]=float(df['rating'][i])
for i in range(0,40303):
if df['rating'][i] >= 6:
df['review'].to_csv("C:\\Users\\rev_pos.csv",encoding='utf8')
这是我得到的错误:
AttributeError: 'str' object has no attribute 'to_csv'
答案 0 :(得分:1)
将代码结尾更改为以下内容:
df.loc[df['rating'][i] >= 6, 'review'].to_csv("C:\\Users\\rev_pos.csv",encoding='utf8')
此代码通过“评分”过滤“评论”列,然后将结果一次全部保存为CSV。
答案 1 :(得分:0)
我无法将其写为注释,但以下是您的代码的一些建议:
import pandas as pd
def user_text(filename):
df = pd.read_csv(filename, sep=';')
return df
import pandas as pd
import numpy as np
...
# assume the columns are called a and b
df = pd.read_csv(filename, sep=';', dtype={'a': np.float32, 'b': np.float32})
for i, row in df.iterrows():
do_something(row)
希望有帮助
答案 2 :(得分:0)
我找到了解决此问题的方法,下面是代码:
for i in range (0,40303):
if df.rating[i] >= 6:
pos_rev.append(df.review[i])
df1=pd.DataFrame(pos_rev)
file2="C:/Users/Amin Chaari/Desktop/pos.csv"
df1.to_csv(file2,sep='\t',encoding='utf8')