简体   繁体   English

使用熊猫加载时,更改csv文件中的值。 (试图了解博客上显示的代码)

[英]Changing the values in csv file when loaded using pandas. (Trying to understand a code presented on a blog)

I wanted to understand the below code. 我想了解以下代码。

First these codes are taken from a blog that I currently read about google BERT. 首先,这些代码取材自我目前阅读的有关google BERT的博客。

https://medium.com/swlh/a-simple-guide-on-using-bert-for-text-classification-bbf041ac8d04 https://medium.com/swlh/a-simple-guide-on-using-bert-for-text-classification-bbf041ac8d04

The dataset can be download in the blog or from the link above. 可以在博客中或从上面的链接下载数据集。

import pandas as pd
train_df = pd.read_csv('data/train.csv', header=None) 
test_df = pd.read_csv("data/test.csv", header=None)
train_df[0] = (train_df[0] == 2).astype(int) #This is the part that I do not understand. I thought this code "(train_df[0] == 2)" will find all the values with "2" but since they did not specify what it should be converted to then how can everything changed from 2 --> 0?
train_df.head()

Current results: 当前结果:

    0   1
0   1   Unfortunately, the frustration of being Dr. Go...
1   0   Been going to Dr. Goldberg for over 10 years. ...
2   1   I don't know what Dr. Goldberg was like before...
3   1   I'm writing this review to give you a heads up...
4   0   All the food is great here. But the best thing...

I just wanted to understand the usage of the code and why it could success so I do not have any expected results. 我只是想了解代码的用法以及为什么它能成功,所以我没有任何预期的结果。

The below code checks if value is 2 or not and converts to int. 下面的代码检查value是否为2并转换为int。 if value is 2 ie it is True and it will be converted to 1. Otherwise it is False and it will be converted to 0. 如果value是2,即它为True,它将转换为1。否则,它为False,它将转换为0。

int() turns the boolean into 1 (True) or 0 (False). int()将布尔值转换为1(真)或0(假)。

train_df[0] = (train_df[0] == 2).astype(int)
>>>t_df[0]
0    1
1    2
2    1
3    1
4    2
Name: 0, dtype: int64
>>>t_df[0]==2
0    False
1     True
2    False
3    False
4     True
Name: 0, dtype: bool
>>>(t_df[0]==2).astype(int)
0    0
1    1
2    0
3    0
4    1
Name: 0, dtype: int64

That code will compare with 2 ( ==2 ) and convert the bool(False, True) values to int (0,1) values ( .astype(int) ) 该代码将与2( == 2比较 并将 bool(False,True)值转换为int(0,1)值( .astype(int)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试使用熊猫读取csv文件时,出现文件未找到错误。 我确定我使用了正确的文件路径 - I am getting a file not found error when trying to read a csv file using pandas. I am sure I have used the correct path to the file 尝试在熊猫中删除列时出现keyError。 - keyError when trying to drop a column in pandas. 使用pandas阅读csv。 为土壤类型重新分配值1到40,为森林覆盖类型重新分配1到7 - Reading csv using pandas. Reassigning values 1 to 40 for soil types, and 1 to 7 for forest cover types 尝试使用 pandas 文件 CSV 文件时出现错误读数 - Getting wrong readings when trying to plot CSV file using pandas 文件未列在目录中,但可以使用 Python / Pandas 从那里写入和加载。 环境问题? - File is not listed in directory, but can be written and loaded from there with Python / Pandas. An environment issue? 熊猫 读取html写入csv - Pandas. read html write to csv 尝试使用 Pandas 打开 CSV 文件未找到 - File Not Found trying to open CSV using Pandas 熊猫。 尝试在条件下删除行但代码用另一列替换列 - Pandas. Trying to delete rows on condition but code replaces column with another column Pandas 保存为 CSV 时更改 NaN 值的格式 - Pandas Changing the format of NaN values when saving to CSV Python熊猫。 替换列中的值 - Python pandas. Replace values in columns
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM