简体   繁体   English

Pandas:使一列的值等于另一列的值

[英]Pandas: Make the value of one column equal to the value of another

Hopefully a very simple question from a Pandas newbie.希望来自 Pandas 新手的一个非常简单的问题。

How can I make the value of one column equal the value of another in a dataframe?如何使数据框中一列的值等于另一列的值? Replace the value in every row.替换每一行中的值。 No conditionals, etc.无条件等。

Context:语境:

I have two CSV's, loaded into dataframe 'a' and dataframe 'b' respectively.我有两个 CSV,分别加载到数据框 'a' 和数据框 'b' 中。

These CSVs are basically the same, except 'a' has a field that was improperly carried forward from another process - floats were rounded to ints.这些 CSV 基本相同,除了 'a' 有一个字段从另一个过程中不正确地结转 - 浮点数四舍五入为整数。 Not my script, can't influence it, I just have the CSVs now.不是我的脚本,无法影响它,我现在只有 CSV。

In reality I probably have 2mil rows and about 60-70 columns in the merged dataframe - so if it's possible to address the columns by their header (in the example these are Col1 and xyz_Col1), that would sure help.实际上,我在合并的数据框中可能有 200 万行和大约 60-70 列 - 因此,如果可以通过列标题(在示例中这些是 Col1 和 xyz_Col1)来解决这些列,那肯定会有所帮助。

I have joined the CSVs on their common field, so now I have a scenario where I have a dataframe that can be represented by the following:我已经在他们的公共领域加入了 CSV,所以现在我有一个场景,我有一个可以由以下内容表示的数据框:

+--------+------+--------+------------+----------+----------+
| CellID | Col1 |  Col2  | xyz_CellID | xyz_Col1 | xyz_Col2 |
+--------+------+--------+------------+----------+----------+
|      1 |    0 | apple  |          1 | 0.23     | apple    |
|      2 |    0 | orange |          2 | 0.45     | orange   |
|      3 |    1 | banana |          3 | 0.68     | banana   |
+--------+------+--------+------------+----------+----------+

The result should be such that Col1 = xyz_Col1:结果应该是 Col1 = xyz_Col1:

+--------+------+--------+------------+----------+----------+
| CellID | Col1 |  Col2  | xyz_CellID | xyz_Col1 | xyz_Col2 |
+--------+------+--------+------------+----------+----------+
|      1 | 0.23 | apple  |          1 | 0.23     | apple    |
|      2 | 0.45 | orange |          2 | 0.45     | orange   |
|      3 | 0.68 | banana |          3 | 0.68     | banana   |
+--------+------+--------+------------+----------+----------+

What I have in code so far:到目前为止我在代码中的内容:

import pandas as pd

a = pd.read_csv('csv1.csv')
b = pd.read_csv('csv2.csv')
#b = b.dropna(axis=1) drop any unnamed fields

#defind 'b' cols by adding an xyz_ prefix as xyz is unique
b = b.add_prefix('xyz_')

#Join the dataframes into a new dataframe named merged
merged = pd.merge(a, b, left_on='Col1', right_on='xyz_Col1')

merged.head(5)

#This is where the xyz_Col1 to Col1 code goes...

#drop unwanted cols
merged = merged[merged.columns.drop(list(merged.filter(regex='xyz')))]

#output to file
merged.to_csv("output.csv", index=False)

Thanks谢谢

merged['col1'] = merged['xyz_Col1']

或者

merged.loc[:, 'col1'] = merged.loc[:, 'xyz_Col1']

暂无
暂无

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

相关问题 根据索引使Pandas Dataframe列等于另一个Dataframe中的值 - Make Pandas Dataframe column equal to value in another Dataframe based on index 如果等于另一列中的值,则熊猫从多列返回值 - Pandas return value from multiple columns if equal to value in another column 根据另一列pandas将列设置为等于值 - Setting column equal to value depending on another column pandas 如果 pandas 列值等于另一列的值,则更新它们 - Update pandas column values if they are equal to another column's value Pandas/Python:根据另一列中的值设置一列的值 - Pandas/Python: Set value of one column based on value in another column 如何为在另一列 pandas 中具有相同值的那些行使一列的值相同 - How to make same value of one column for those rows which have same values in another column pandas 从一列中删除等于另一列中的值的值 - Remove values from one column, that are equal to the value in another 如果列等于 Pandas 中的值,如何使当前行值等于前一行值? - How to make current row value equal to previous row value if a column equals a value in Pandas? 检查一列值是否在另一列中并创建列以在 Pandas 中指示 - Check if one column value is in another column and create column to indicate in Pandas 如果另一列中的唯一值数等于给定值,如何按 Pandas 列进行过滤 - How to filter by a pandas column if the number of unique value in another column is equal a given value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM