使用现有列从另一个数据帧创建列

发布于 2025-02-10 14:36:31 字数 684 浏览 0 评论 0 原文

“单元”列的pandas

    Quantity    Unit
0   Current     A
1   Current     mA
2   Voltage     V
3   Length      m
4   Length      km

数据

      Current  Voltage  Length           
A       1        NaN      NaN
mA     1000      NaN      NaN
V      NaN       1        NaN
m      NaN       NaN      1000
km     NaN       NaN       1 

    Quantity    Unit   Ratio
0   Current     A        1
1   Current     mA      1000
2   Voltage     V        1
3   Length      m        1
4   Length      km      1000

我有一个带有“数量”和 iTerrows的每个值,但我确实有大数据框架,并且在时间的情况下无法正常工作。 也许还有另一种更方便的方法可以解决我的问题,但是我必须使用比率数据框架。

I have a pandas DataFrame with columns "Quantity" and "Unit", e.g.:

    Quantity    Unit
0   Current     A
1   Current     mA
2   Voltage     V
3   Length      m
4   Length      km

and I have a pandas DataFrame with ratios of units, e.g.:

      Current  Voltage  Length           
A       1        NaN      NaN
mA     1000      NaN      NaN
V      NaN       1        NaN
m      NaN       NaN      1000
km     NaN       NaN       1 

Is it possible to create in first dataframe column with values of ratios?:

    Quantity    Unit   Ratio
0   Current     A        1
1   Current     mA      1000
2   Voltage     V        1
3   Length      m        1
4   Length      km      1000

I have tried to get every value by iterrows, but I have really big dataframe and it's not working correctly in case of time.
Maybe there is another, more expedient way to solve my issue, but I have to work with dataframes of ratios.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

日久见人心 2025-02-17 14:36:31

pandas' merge()有效地用作映射数据从一个df到另一个df

# stack the second df and merge it with the first on Unit and Quantity
df1.merge(df2.stack().rename('Ratio'), left_on=['Unit', 'Quantity'], right_index=True)

“在此处输入图像说明”

Pandas' merge() efficiently works as mapping data from one df to another

# stack the second df and merge it with the first on Unit and Quantity
df1.merge(df2.stack().rename('Ratio'), left_on=['Unit', 'Quantity'], right_index=True)

enter image description here

离旧人 2025-02-17 14:36:31

使用 to_dict

d = df2.to_dict('i')

df['Ratio'] = df.apply(lambda s: d[s['Unit']][s['Quantity']], axis=1)

  Quantity Unit   Ratio
0  Current    A     1.0
1  Current   mA  1000.0
2  Voltage    V     1.0
3   Length    m  1000.0
4   Length   km     1.0

Using to_dict:

d = df2.to_dict('i')

df['Ratio'] = df.apply(lambda s: d[s['Unit']][s['Quantity']], axis=1)

  Quantity Unit   Ratio
0  Current    A     1.0
1  Current   mA  1000.0
2  Voltage    V     1.0
3   Length    m  1000.0
4   Length   km     1.0
著墨染雨君画夕 2025-02-17 14:36:31

给定输入数据框

df
      Current  Voltage  Length           
A       1        NaN      NaN
mA     1000      NaN      NaN
V      NaN       1        NaN
m      NaN       NaN      1000
km     NaN       NaN       1 

,那么您似乎想要的东西像

df.stack()

A   Current       1.0
mA  Current    1000.0
V   Voltage       1.0
m   Length     1000.0
km  Length        1.0
dtype: float64

Given the input dataframe

df
      Current  Voltage  Length           
A       1        NaN      NaN
mA     1000      NaN      NaN
V      NaN       1        NaN
m      NaN       NaN      1000
km     NaN       NaN       1 

Then it seems like you want something as simple as

df.stack()

A   Current       1.0
mA  Current    1000.0
V   Voltage       1.0
m   Length     1000.0
km  Length        1.0
dtype: float64
花海 2025-02-17 14:36:31

您可以使用 MAP

df.assign(Ratio = df.Unit.map(ratios.bfill(axis = 1).Current))

  Quantity Unit   Ratio
0  Current    A     1.0
1  Current   mA  1000.0
2  Voltage    V     1.0
3   Length    m  1000.0
4   Length   km     1.0

You can use map:

df.assign(Ratio = df.Unit.map(ratios.bfill(axis = 1).Current))

  Quantity Unit   Ratio
0  Current    A     1.0
1  Current   mA  1000.0
2  Voltage    V     1.0
3   Length    m  1000.0
4   Length   km     1.0
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文