简体   繁体   English

熊猫Dataframe操纵

[英]Panda Dataframe manipulation

Can anyone explain to me how to solve this problem.谁能向我解释如何解决这个问题。

Let's say i have a dataframe like:假设我有一个 dataframe,例如:

Df = {'toto': ["A", "B", "C", "D"], 
      'titi': ["g", "t", "x", "z"],
      'Energy': [180, 345, 234, 654],
      'T10sec': [0.1, 0.4, 0.5, 1], 
      'T50sec': [5.3, 5.7, 8, 2]}

For this Df i've created a function like对于这个Df ,我创建了一个 function 之类的

def record(letter1, letter2, Energy_value, time):

What i want to do it's我想做的是

Check if: the enter values are in this Df , if yes then检查是否:输入值在此Df中,如果yes ,则

Results = Df.thecorrespondingtime[i]

Assume that for the columns 'toto' and 'titi' the input letter are in Df , two cases are possible:假设对于'toto''titi'列,输入字母在Df中,可能有两种情况:

Case1: the input 'Energy_value' is in Df but not the 'time' value then i have to find the range of this value and create its column and fill it with interpolation.案例 1:输入'Energy_value'Df中但不是'time'值然后我必须找到该值的范围并创建其列并用插值填充它。

Case 2: the column value of 'time' exists but the input Energy value no.情况 2: 'time'的列值存在,但输入的 Energy 值没有。 Here again i need to do an interpolation.在这里我又需要做一个插值。

My limitations are: How to check if the 'time' column exists and how to put between two existing values.我的限制是:如何检查'time'列是否存在以及如何将两个现有值放在一起。 The same question for the input Energy value.输入能量值的相同问题。

Here, what i tried:在这里,我尝试了什么:

def record(letter1, letter2, Energy_value, time='T15sec'):
    For i in range(len(Df)):
        If ((df.toto[i] == letter1) and 
            (df.titi[i] == letter2) and 
            (df.Energy[i] == Energy_value) and 
            (time in df)):

But it doesn't work.但它不起作用。 Can anyone help me?谁能帮我?

If the question is basically about inserting another time column into the dataframe this would be a solution:如果问题基本上是关于将另一个时间列插入 dataframe 这将是一个解决方案:

import pandas as np

df = pd.DataFrame({'toto': ["A", "B", "C", "D"], 
      'titi': ["g", "t", "x", "z"],
      'Energy': [180, 345, 234, 654],
      'T10sec': [0.1, 0.4, 0.5, 1], 
      'T50sec': [5.3, 5.7, 8, 2]})
df
    toto    titi    Energy  T10sec  T50sec
0   A       g       180     0.1     5.3
1   B       t       345     0.4     5.7
2   C       x       234     0.5     8.0
3   D       z       654     1.0     2.0

Add time column:添加时间栏:

import numpy as np
time = 'T15sec'

if not time in df:
    df[time] = np.NaN

df.iloc[:, 3:] = df.iloc[:, 3:].T.sort_index().interpolate().T

df[['toto', 'titi', 'Energy', 'T10sec', time, 'T50sec']]

    toto    titi    Energy  T10sec  T15sec  T50sec
0   A       g       180     0.1     2.70    5.3
1   B       t       345     0.4     3.05    5.7
2   C       x       234     0.5     4.25    8.0
3   D       z       654     1.0     1.50    2.0

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

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