简体   繁体   English

在 if 循环中重命名 pandas 条目

[英]Rename pandas entries within if-loop

I got a dataframe that looks like that:我有一个看起来像这样的 dataframe:

 appended_data
...
...
Out[24]: 
         Unnamed: 0  Region     GeneID  DistanceValue
229676       229676     8.0   69013161       0.972237
229677       229677     8.0   75931496       0.978386
229678       229678     8.0   73424023       0.982667
229679       229679     8.0   74736195       0.987057
280716       280716    10.0   69782778       0.971998
            ...     ...        ...            ...
1097355     1097355    42.0     633685       0.972917
1097356     1097356    42.0   68301216       0.974058
1097357     1097357    42.0  100146162       0.977017
1097358     1097358    42.0   74734794       0.985398
1097359     1097359    42.0     633694       0.992054

[136 rows x 4 columns]

I want to rename the entries in Region in a if-loop:我想在 if 循环中重命名 Region 中的条目:

Like.. region 42 should become the name of the 42. entry of layernames. Like.. region 42 应该成为 layernames 的 42. entry 的名称。 I tried:我试过了:

import pandas as pd
appended_data = pd.read_csv('/home/anja/0415_pyversion.csv')
with open('/home/anja/Schreibtisch/Master/ABA/layer2_names.txt') as f:
    layernames = [line.strip() for line in f.read().split('\n')]


for i in range(43):
    if (appended_data.Region == i).all:
     (appended_data.Region == i).all = layernames[i] 

I tried that, I got no error message, but my entries aren't renamed.我试过了,我没有收到错误消息,但我的条目没有重命名。

.. if do ..如果这样做

print((appended_data.Region == i).all)

I get:我得到:

Name: Region, Length: 136, dtype: bool>
<bound method Series.all of 229676     False
229677     False
229678     False
229679     False
280716     False

1097355     True
1097356     True
1097357     True
1097358     True
1097359     True
Name: Region, Length: 136, dtype: bool>

Series.map

If you ever use for i in range(..) as an iterator then alarm bells should go off as Python as a built in iterator which can be accessed via enumerate如果您曾经使用for i in range(..)作为迭代器,那么警报应该 go 关闭为 Python 作为内置迭代器,可以通过enumerate访问

You've not given a minimum example of what layer names is, but it seems like it's a list.您没有给出图层名称的最小示例,但它似乎是一个列表。

we can leverage the map function with a dictionary and make a one time operation to replace all the region name.s我们可以利用带有字典的 map function 并进行一次性操作来替换所有区域名称。

Setup设置

import pandas as pd

s = ['a','b','c'] # your layer_name list. 

layer_dict = {k : v for (k,v) in enumerate(s)}
# {0: 'a', 1: 'b', 2: 'c'}
df = pd.DataFrame([0,1,2],columns=list('A'))

In Action:在行动:

df['B'] = df['A'].map(layer_dict)
print(df)
   A  B
0  0  a
1  1  b
2  2  c

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM